因此,我的任务(我必须使用while语句)是制作数字猜谜游戏,其中一部分是显示玩家获得数字后的猜测数量。我发现我读过的东西应该可以工作但不会。这是我的代码。
#A text program that is a simple number guessing game.
import time
import random
#Setting up the A.I.
Number = random.randint(1,101)
def AI():
B = AI.counter =+ 1
Guess = int(input("Can you guess what number I'm Thinking of?: "))
while Guess > Number:
print("nope, to high.")
return AI()
while Guess < Number:
print("Sorry, thats to low. try again!")
return AI()
while Guess == Number:
print("Congragulations! you win! You guessed " + str(B) + " times")
time.sleep(60)
quit()
AI.counter = 0
AI()
虽然当玩家获得正确的数字时,它表示即使不是这样,玩家也会猜到这一点。
使用默认参数:
def AI(B=1):
Guess = int(input("Can you guess what number I'm Thinking of?: "))
while Guess > Number:
print("nope, to high.")
return AI(B + 1)
while Guess < Number:
print("Sorry, thats to low. try again!")
return AI(B + 1)
while Guess == Number:
print("Congragulations! you win! You guessed " + str(B) + " times")
time.sleep(60)
return
然后调用函数:
AI()
另外,你应该在这里使用if
s而不是while
s,因为循环只运行一次,但是while
s也可以工作,所以没关系。此外,递归可能会占用你的RAM,这只会浪费资源,只要你可以实现与循环相同的东西,但你的方法是有效的,所以这也很好。
你非常接近@Simpson!这是一个稍微改变的版本,可以给你你想要的:)
如果您有任何疑问,请告诉我!
#A text program that is a simple number guessing game.
import random
#Setting up the A.I.
def AI():
counter = 1
number = random.randint(1,101)
guess = int(input("Can you guess what number I'm Thinking of?: "))
while guess != number:
if guess < number:
print("Sorry, thats to low. try again!")
else:
print("nope, too high")
counter += 1
guess = int(input("Can you guess what number I'm Thinking of?: "))
print("Congragulations! you win! You guessed " + str(counter) + " times")
time.sleep(60)
quit()
AI()
没有递归 - 将whiles更改为ifs并在方法内添加了counter。
import time
import random
Number = random.randint(1,101)
def AI():
B = 1
Guess = int(input("Can you guess what number I'm Thinking of?: "))
while True:
if Guess > Number:
print("nope, to high.")
elif Guess < Number:
print("Sorry, thats to low. try again!")
if Guess == Number:
print("Congragulations! you win! You guessed " + str(B) + " times")
time.sleep(2)
break # leave the while true
# increment number
B += 1
Guess = int(input("Can you guess what number I'm Thinking of?: "))
AI()
计算函数调用是function decorators的一个完美案例,def call_counted(funct):
def wrapped(*args, **kwargs):
wrapped.count += 1 # increase on each call
return funct(*args, **kwargs)
wrapped.count = 0 # keep the counter on the function itself
return wrapped
是Python的一个非常有用的功能。您可以将装饰器定义为:
import time
import random
secret_number = random.randint(1, 101)
@call_counted # decorate your AI function with the aforementioned call_counted
def AI():
current_guess = int(input("Can you guess what number I'm thinking of?: "))
while current_guess > secret_number:
print("Nope, too high. Try again!")
return AI()
while current_guess < secret_number:
print("Sorry, that's too low. Try again!")
return AI()
while current_guess == secret_number:
print("Congratulations! You win! You guessed {} times.".format(AI.count))
time.sleep(60)
quit()
AI()
然后,您可以使用它来装饰您希望计算调用的函数,而无需在流程流中处理计数器本身:
import time
import random
secret_number = random.randint(1, 101)
def AI():
counter = 0
while True:
counter += 1
current_guess = int(input("Can you guess what number I'm thinking of?: "))
if current_guess > secret_number:
print("Nope, too high. Try again!")
elif current_guess < secret_number:
print("Sorry, that's too low. Try again!")
else:
break
print("Congratulations! You win! You guessed {} times.".format(counter))
time.sleep(60)
quit()
AI()
我重新调整了你的代码,但它基本上是一样的。
我会避免递归,因为这可以写得更简单,而且不需要计算函数调用:
PEP 8 - Style Guide for Python Code
您可以使用函数装饰器进行基因操作,该函数装饰器将调用计数器添加到应用它的任何函数:
(注意我也修改了你的代码,所以它更接近""" A text program that is a simple number guessing game. """
import functools
import time
import random
def count_calls(f):
""" Function decorator that adds a call count attribute to it and counts
the number of times it's called.
"""
f.call_count = 0
@functools.wraps(f)
def decorated(*args, **kwargs):
decorated.call_count += 1
return f(*args, **kwargs)
return decorated
# Setting up the A.I.
number = random.randint(1, 101)
# print('The number is:', number) # For testing.
@count_calls
def AI():
guess = int(input("Can you guess what number I'm thinking of?: "))
while guess > number:
print("Nope, too high.")
return AI()
while guess < number:
print("Sorry, that's too low. Try again!")
return AI()
while guess == number:
print("Congragulations! You win! You guessed " + str(AI.call_count) + " times")
time.sleep(10)
quit()
AI()
。)
qazxswpoi