Is this possible with pyhton? If it is, how can you do it?

def raise():
    raise = raw_input('Do you really want to raise? y/n')
    if raise == 'y':
        bet = bet+1
        return # Should RETURN to play(), after raise()
               # Like "goto LABEL"
    else:
        return

def play():
     bet = 1
     print 'The computer did a bet of 1 dollar.'
     print 'I raise.'
     raise()  # The 'LABEL'
     print 'The bet now is', bet
     print 'End of the test."

Dani AI

Generated

A few practical points that build on and : Python has no goto, so the right patterns are to return an updated value, keep state in an object, or mutate a shared object. used the return-value approach and rightly suggested validating input. Both are fine; choose the approach that keeps the game state easy to reason about.

Avoid naming a function raise. raise is a Python statement for exceptions and using it as an identifier will either be illegal or confusing. See the official list of Python keywords for reference: Python keywords. Pick clear names like offer_raise, ask_raise, or try_raise.

An alternative that keeps state tidy is a small object with methods that validate input and update bet. This keeps bet as an attribute instead of passing it around:

class PokerRound:
    def __init__(self, bet=1):
        self.bet = bet

    def ask_raise(self):
        while True:
            ans = input("Do you want to raise? (y/n) ").strip().lower()
            if ans in ("y", "n"):
                break
            print("Please type y or n.")
        if ans == "y":
            self.bet += 1

    def play(self):
        print("Computer bet:", self.bet)
        print("Player considers raising.")
        self.ask_raise()
        print("Bet now is", self.bet)

if __name__ == "__main__":
    PokerRound().play()

If you ever need to modify an outer variable inside a nested function, prefer explicit returns or a container; nonlocal (Python 3) or global are available but more error-prone—see the docs for the nonlocal/global statements. Follow simple naming and input-validation practices (PEP 8 is a good style guide) and prefer input() in modern Python.

Recommended Answers

All 4 Replies

Here is your code, slightly modified to work. I have passed bet in as a parameter and returned it; and renamed the function to something legal (I recommend using a better name than my choice). Note also the space after the y/n: Not necessary, but much prettier in my opinion.

def rraise(bet):
  rraise = raw_input('Do you really want to raise? y/n ')
  if rraise == 'y':
    bet = bet+1
  return bet
#
def play():
  bet = 1
  print 'The computer did a bet of 1 dollar.'
  print 'I raise.'
  bet = rraise(bet)
  print 'The bet now is', bet
  print 'End of the test.'

play()

You also need some error checking, it's good practice.
Example:

rraise = raw_input("Do you really want to raise? y/n ')
if rraise == 'y':
  bet += 1
elif rraise == 'n':
  pass
else: print "y or n please..."
return bet

Thank you both. It worked. Cant mark my thread as solved, but it is.

Mark thread is solved has moved. You can now find it as a link at the top. Use ctrl+F and search for solved and you should find it. It is next to the bit that says "Python Discussion Thread View First Unread"

Just an FYI for anyone who needs to know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.