[英]How do I keep track of a user's guesses in Python? attempts= attempts + 1 is not working
我需要跟蹤用戶在簡單的猜測游戲中輸入的猜測數量。
我嘗試使用trys = 0,然后將trys設置為= trys + 1。即使執行此操作,即使用戶猜測的嘗試次數超過一次,代碼也會打印“您已經嘗試了1次”。
碼:
attempts = 0;
print("Hello, welcome to the game. You will be choosing a number
between 1 and 100. You can only guess up to 10 times.")
for tries in range(tries_allowed):
print("You only get 10 tries.")
break
while attempts < 10:
guess = int(input("Please guess a number"));
attempts_used= attempts + 1;
if guess > random_number:
print("Guess is too high, try a smaller number");
elif guess < random_number:
print("Guess is too low, try a higher number");
elif guess == random_number:
attempts_used=str(attempts_used)
print("Correct- you win in", attempts_used, "guesses");
exit();
else:
if tries_allowed == 10:
print("You failed to guess in time")
my_list= [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
my_list.append(attempts_used)
print(my_list)
您永遠不會更新attempts
變量,您已經創建了一個名為attempts_used
的新變量,您不需要這樣做。
只在attempts
使用的地方使用attempts_used
注意:在執行此操作時,您應該擺脫while循環中所謂的“魔術數字”或硬編碼限制
while attempts < tries_allowed:
稍微整理一下代碼,顯示+=
計數方法可用於您的腳本。 就像其他人所說的那樣,原始代碼正在創建一個全新的變量attempts_used
,它只是attempts + 1
,而attempts
仍然為0。
也可能是attempts = attempts + 1
, +=
表示同一件事。
為了使int
一個str
的Python為打印目的,它並不需要被存儲到一個獨立的變量,只需調用str()
圍繞它,除非你打算單獨使用的字符串。
import random
random_number = random.randint(1,100)
attempts = 0
tries_allowed = 10
print("Hello, welcome to the game. You will be choosing a number between 1 and 100")
print("You only get " + str(tries_allowed) + " tries.")
my_list = []
while attempts < tries_allowed:
guess = int(input("Please guess a number: "))
if guess in my_list:
print("You have already guessed " + str(guess))
continue
attempts += 1
my_list.append(guess)
if guess > random_number:
print("Guess is too high, try a smaller number")
elif guess < random_number:
print("Guess is too low, try a higher number")
elif guess == random_number:
print("Correct- you win in", str(attempts), "guesses")
break
else:
if attempts == 10:
print("You failed to guess in time")
for item in my_list:
print(item)
您的“ attemps”變量保持為0,因此attemps_used(attempts + 1)始終為1。您必須將兩個變量合並為一個變量才能對其進行控制(attempts = attempts + 1)
該代碼還將檢查先前的輸入並顯示一條消息。
import random
# Hello World program in Python
attempts = 0
print("Hello, welcome to the game. You will be choosing a number between 1 and 100. You can only guess up to 10 times.")
random_number = random.randint(1, 101)
#print(random_number)
my_list= []
for tries in range(10):
print("You only get 10 tries.")
guess = int(input("Please guess a number: "))
if guess in my_list:
print("You already guessed this number!!")
my_list.append(guess)
attempts += 1
if guess > random_number:
print("Guess is too high, try a smaller number")
elif guess < random_number:
print("Guess is too low, try a higher number")
else:
print("Correct- you win in", tries + 1, "guesses")
attempts = -1
break
if attempts is 10 and attempts is not -1:
print("You failed to guess in time")
print("Attempts : ", my_list)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.