![](/img/trans.png)
[英]How do I keep track of a user's guesses in Python? attempts= attempts + 1 is not working
[英]How do I make Python 3.4 keep score of the amount of guesses made?
我正在用Python做一个Guess the Number游戏,我想让Python记分您猜对数字之前花了多少次。 我将如何去做呢? 如果需要,我可以发布代码进行查看。 谢谢。
import time
import os
from random import randrange, uniform
#Difficulty - Easy
def easy():
print ("")
print ("Difficulty: Easy")
print ("")
irand = randrange(1,10)
with open("GTN.txt", "a") as text_file:
text_file.write(str(irand) + " Easy" + "\n")
while True:
number = input("Pick a number 1 - 10: ")
try:
number = int(number)
except ValueError:
print(" ")
print(number, 'is not a number, try again.')
continue
if number > irand:
print("That's too high, try again.")
print(" ")
time.sleep(1)
elif number < irand:
print("That's too low, try again.")
print(" ")
time.sleep(1)
elif number == irand:
print(" ")
print("You got it right! You won!")
print(" ")
time.sleep(2)
main()
break
#Difficulty - Medium
def medium():
print ("")
print ("Difficulty: Medium")
print ("")
irand = randrange(1,100)
with open("GTN.txt", "a") as text_file:
text_file.write(str(irand) + " Medium" + "\n")
while True:
number = input("Pick a number 1 - 100: ")
try:
number = int(number)
except ValueError:
print(" ")
print(number, 'is not a number, try again.')
continue
if number > irand:
print("That's too high, try again.")
print(" ")
time.sleep(1)
elif number < irand:
print("That's too low, try again.")
print(" ")
time.sleep(1)
elif number == irand:
print(" ")
print("You got it right! You won!")
print(" ")
time.sleep(2)
main()
break
#Difficulty - Hard
def hard():
print ("")
print ("Difficulty: Hard")
print ("")
irand = randrange(1,1000)
with open("GTN.txt", "a") as text_file:
text_file.write(str(irand) + " Hard" + "\n")
while True:
number = input("Pick a number 1 - 1,000: ")
try:
number = int(number)
except ValueError:
print(" ")
print(number, 'is not a number, try again.')
continue
if number > irand:
print("That's too high, try again.")
print(" ")
time.sleep(1)
elif number < irand:
print("That's too low, try again.")
print(" ")
time.sleep(1)
elif number == irand:
print(" ")
print("You got it right! You won!")
print(" ")
time.sleep(2)
main()
break
#Difficulty - Ultra
def ultra():
print ("")
print ("Difficulty: Ultra")
print ("")
irand = randrange(1,100000)
with open("GTN.txt", "a") as text_file:
text_file.write(str(irand) + " Ultra" + "\n")
while True:
number = input("Pick a number 1 - 100,000: ")
try:
number = int(number)
except ValueError:
print(" ")
print(number, 'is not a number, try again.')
continue
if number > irand:
print("That's too high, try again.")
print(" ")
time.sleep(1)
elif number < irand:
print("That's too low, try again.")
print(" ")
time.sleep(1)
elif number == irand:
print(" ")
print("You got it right! You won!")
print(" ")
time.sleep(2)
main()
break
#Difficulty - Master
def master():
print ("")
print ("Difficulty: Master")
print ("")
irand = randrange(1,1000000)
with open("GTN.txt", "a") as text_file:
text_file.write(str(irand) + " Master" + "\n")
while True:
number = input("Pick a number 1 - 1,000,000: ")
try:
number = int(number)
except ValueError:
print(" ")
print(number, 'is not a number, try again.')
continue
if number > irand:
print("That's too high, try again.")
print(" ")
time.sleep(1)
elif number < irand:
print("That's too low, try again.")
print(" ")
time.sleep(1)
elif number == irand:
print(" ")
print("You got it right! You won!")
print(" ")
time.sleep(2)
main()
break
#This is the MainMenu
def main():
time.sleep(2)
while True:
print ("Please select a difficulty when prompted!")
time.sleep(1)
print ("[1] Easy")
time.sleep(1)
print ("[2] Medium")
time.sleep(1)
print ("[3] Hard")
time.sleep(1)
print ("[4] Ultra")
time.sleep(1)
print ("[5] Master")
time.sleep(1)
print ("[6] Exit")
print ("")
time.sleep(1)
choice = input ("Please Choose: ")
if choice == '1':
time.sleep(2)
print ("")
print ("Loading game...")
time.sleep(2)
easy()
elif choice == '2':
time.sleep(2)
print ("")
print ("Loading game...")
time.sleep(2)
medium()
elif choice == '3':
time.sleep(2)
print ("")
print ("Loading game...")
time.sleep(2)
hard()
elif choice == '4':
time.sleep(2)
print ("")
print ("Loading game...")
time.sleep(2)
ultra()
elif choice == '5':
time.sleep(2)
print ("")
print ("Loading game...")
time.sleep(2)
master()
elif choice == '6':
time.sleep(2)
print ("")
print ("Exiting the game!")
print ("")
print ("3")
time.sleep(0.5)
print ("2")
time.sleep(0.5)
print ("1")
time.sleep(2)
SystemExit
else:
print ("Invalid Option: Please select from those available.")
print("")
time.sleep(1)
print ("Welcome to GTN!")
time.sleep(2)
print ("Developed by: oysterDev")
time.sleep(2)
print ("Version 1.1.0")
print (" ")
main()
@Benjamin的答案会起作用。 但是要回答有关如何开始执行DRY的问题,您可以执行以下操作:
您的整个主要游戏代码都可以进入此功能,并使用一些定义硬度的关键参数:
def the_game(difficulty_name, range_start, range_end):
score = 0
print ("")
print ("Difficulty: %s" % difficulty_name)
print ("")
irand = randrange(range_start, range_end)
with open("GTN.txt", "a") as text_file:
text_file.write(str(irand) + " %s" % difficulty_name + "\n")
while True:
number = input("Pick a number 1 - 10: ")
try:
number = int(number)
except ValueError:
print(" ")
print(number, 'is not a number, try again.')
continue
if number > irand:
print("That's too high, try again.")
print(" ")
score += 1
time.sleep(1)
elif number < irand:
print("That's too low, try again.")
print(" ")
score += 1
time.sleep(1)
elif number == irand:
print(" ")
print("You got it right! You won!")
print(" ")
print("You guessed wrong " + str(score) + " times")
time.sleep(2)
main()
break
然后,您可以定义一些小功能,这些功能可以根据用户选择的硬度级别来调用游戏,如下所示:
#Difficulty - Easy
def easy():
the_game("Easy", 1, 10)
#Difficulty - Medium
def medium():
the_game("Medium", 1, 100)
#Difficulty - Hard
def hard():
the_game("Hard", 1, 1000)
#Difficulty - Ultra
def ultra():
the_game("Ultra", 1, 100000)
#Difficulty - Master
def master():
the_game("Master", 1, 1000000)
最后,您可以像这样定义主要功能:
#This is the MainMenu
def main():
time.sleep(2)
while True:
print ("Please select a difficulty when prompted!")
time.sleep(1)
print ("[1] Easy")
time.sleep(1)
print ("[2] Medium")
time.sleep(1)
print ("[3] Hard")
time.sleep(1)
print ("[4] Ultra")
time.sleep(1)
print ("[5] Master")
time.sleep(1)
print ("[6] Exit")
print ("")
time.sleep(1)
choice = input ("Please Choose: ")
def show_loading_screen():
time.sleep(2)
print ("")
print ("Loading game...")
time.sleep(2)
def show_exit_screen():
time.sleep(2)
print ("")
print ("Exiting the game!")
print ("")
print ("3")
time.sleep(0.5)
print ("2")
time.sleep(0.5)
print ("1")
time.sleep(2)
if choice == '1':
show_loading_screen()
easy()
elif choice == '2':
show_loading_screen()
medium()
elif choice == '3':
show_loading_screen()
hard()
elif choice == '4':
show_loading_screen()
ultra()
elif choice == '5':
show_loading_screen()
master()
elif choice == '6':
show_exit_screen()
SystemExit
else:
print ("Invalid Option: Please select from those available.")
print("")
time.sleep(1)
您会发现我们已经将重复的屏幕加载代码行提取到内联函数中,可以重复使用。
最后,如果该Python文件作为脚本执行,则可以调用此主要函数。 这是一个好习惯。 您可以这样做:
if __name__ == "__main__":
print ("Welcome to GTN!")
time.sleep(2)
print ("Developed by: oysterDev")
time.sleep(2)
print ("Version 1.1.0")
print (" ")
main()
希望这对了解如何开始对DRY进行重构很有帮助。
尝试制作一个名为ex的变量。 “得分”,每当他们猜错的时候加1。
这应该工作。
def easy():
score = 0
print ("")
print ("Difficulty: Easy")
print ("")
irand = randrange(1,10)
with open("GTN.txt", "a") as text_file:
text_file.write(str(irand) + " Easy" + "\n")
while True:
number = input("Pick a number 1 - 10: ")
try:
number = int(number)
except ValueError:
print(" ")
print(number, 'is not a number, try again.')
continue
if number > irand:
print("That's too high, try again.")
print(" ")
score += 1
time.sleep(1)
elif number < irand:
print("That's too low, try again.")
print(" ")
score += 1
time.sleep(1)
elif number == irand:
print(" ")
print("You got it right! You won!")
print(" ")
print("You guessed wrong " + str(score) + " times")
time.sleep(2)
main()
break
希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.