是否有办法使这项工作奏效,或者我将不得不找到另一种逻辑方式?我写得很快,以作为我遇到的问题的一个例子。每次运行guess函数时,我想添加一个猜测。问题在于,当我退出该函数然后重新输入时,猜测计数器将重置为0。而且我无法在已定义的函数之外获取变量“ guesses”,这就是我的困惑。
什么是正确的方法?
this.props.likedJobs.map()
我希望能够不重新设置guesss变量而离开函数并重新输入。
答案 0 :(得分:1)
一种常见的处理方法是将您的信息封装在一个类的实例中。该实例将包含属性,例如猜测数和您要维护的任何其他状态。这些方法将创建包括操纵这些属性的行为。
您可以在此处创建一个新实例,包括传递初始猜测数和答案的选项:
class Guess:
def __init__(self, answer = 2, guesses = 5): # 2 and 5 are deafaults if nothing is passed in
self.guesses = guesses
self.answer = answer
def guess(self):
guess = input("Guess: ")
if guess == self.answer:
print("you win")
else:
self.guesses -= 1
print(f"You have {self.guesses} left")
self.movement()
def movement(self):
choice = input("left or guess? ")
if choice == "left":
self.movement()
if choice == "guess":
self.guess()
g = Guess(answer = 5, guesses = 2) # make a new game using the passed in values
g.movement() # start game
答案 1 :(得分:0)
我喜欢OOP(面向对象编程),所以我会选择另一个答案。
话虽这么说,Python对此有一个有用的东西称为generator
。将其视为记住状态的函数。
def my_gen():
x = 0
while True:
# Yield acts just like a normal return, the functions stops
# after returning x.
yield x
# Next time the generator is called it will resume
# right here, add it will remember all the values
# it previously had (i.e. it remembers the last value for x.
x += 1
# Note a generator is called differently then a normal function
# then a normal function
g = my_gen()
print(next(g)) # prints 0
print(next(g)) # prints 1
print(next(g)) # prints 2
还介绍了发电机如何停止:
def my_gen2():
x = 2
while x > 0:
yield x
x -= 1
# Note that when a generator function
# has no more yields it will throw a
# StopIteration Exception
g = my_gen2()
print(next(g)) # prints 2
print(next(g)) # prints 1
print(next(g)) # This will cause an StopIteration Exception
# you can get around this either by:
g = my_gen2()
for x in g: # A for loop automatically stops on StopIteration
print(x)
# Or catch the Exception
try:
g = my_gen2()
for _ in range(5): # Just calling next enough times for it to break
print(next(g))
except StopIteration:
print("can't call generator any more")
您的代码:
def guess():
x = 5
guesses = 0
num_tries = 1
while guesses < num_tries:
guess = input("Guess: ")
if guess == x:
print("You win")
yield 0
else:
guesses += 1
if guesses == num_tries:
yield 0 # Game over
else:
print("Try again")
yield 1 # Game can continue
# Don't add unneeded recusion.
# Python has a limited stack. Don't consume it
# for something you should do in a loop.
def play():
g = guess()
while True:
choice = input("left or guess: ")
print(choice)
if choice == "left":
continue
elif choice == "guess":
try:
if not next(g):
print("Game over")
break
except StopIteration:
print("Somehow called to many times")
break
else:
print("Invalid Entry")
play()
答案 2 :(得分:0)
def guess(guesses = 0):
x = 5
choice = input("left or guess")
guesses = guesses + 1
if choice == "left":
return guess(guesses=guesses)
elif choice == "guess":
guessint = int(input("Guess(int): "))
if guessint == x:
print("You win")
print('number:', guesses)
return guesses
else:
print("try again")
return guess(guesses=guesses)
guess()