我一直在与合作伙伴一起为课堂作业编写一个程序,而且我们一直坚持要添加一个" q来退出"在里面。作业是做一个猜测数字"游戏。这是我们的代码:
import random
def get_distance(secret,guess):
difference = abs(secret - guess)
return difference
def point_machine(guess1,guess2):
points_1 = 0
points_2 = 0
if guess1 < guess2:
print ("Player 1 wins")
points_1 += 1
elif guess2 < guess1:
print ("Player 2 wins")
points_2 += 1
elif guess1 == guess2:
print ("It's a tie!")
points_1 += 1
points_2 += 1
return points_1, points_2
x = 0
print ("Welcome to Guess-the-Number!")
print ("Press 'q' to quit at any time.")
name1 = input("What is player one's name?: ")
name2 = input("What is player two's name?: ")
rounds = (input("How many rounds?: "))
points_1 = 0
points_2 = 0
for i in range(int(rounds)):
x += 1
secret = random.randint(0,100)
print ("Round",x)
print ("~~~~~~~")
guess1 = int(input(name1+", take a guess: "))
guess1 = get_distance(secret,guess1)
guess2 = int(input(name2+", take a guess: "))
guess2 = get_distance(secret,guess2)
new1,new2=point_machine(guess1,guess2)
points_1 += new1
points_2 += new2
print ("The number was:",secret)
print ("Score:",points_1,"to",points_2)
if points_1 > points_2:
print (name1,"wins the game!")
elif points_2 > points_1:
print (name2,"wins the game!")
elif points_1 == points_2:
print ("It's a tie!")
print (points_1,"to",points_2)
要求是当&#34; q&#34;在游戏过程中随时输入,它会返回到主菜单&#34;。
提前致谢
答案 0 :(得分:2)
您需要在每个input()命令中进行测试。所以而不是:
guess1 = int(input(name1+", take a guess: "))
guess1 = get_distance(secret,guess1)
guess2 = int(input(name2+", take a guess: "))
guess2 = get_distance(secret,guess2)
试试这个:
userInput = input(name1+", take a guess: ")
if userInput == "q":
exit()
guess1 = int(userInput)
guess1 = get_distance(secret,guess1)
userInput = input(name2+", take a guess: ")
if userInput == "q":
exit()
guess2 = int(userInput)
guess2 = get_distance(secret,guess2)
这样,用户可以随时键入&#34; q&#34;并按Enter键退出游戏。
或者,您可以定义自己的函数来代替input()函数:
def userInput(prefix):
a = input(prefix)
if a=="q":
exit()
else:
return a
然后:
guess1 = int(userInput(name1+", take a guess: "))
guess1 = get_distance(secret,guess1)
guess2 = int(userInput(name2+", take a guess: "))
guess2 = get_distance(secret,guess2)
答案 1 :(得分:0)
您可以使用pythoncom
,pyHook
import pythoncom, pyHook
import random
def get_distance(secret,guess):
difference = abs(secret - guess)
return difference
def point_machine(guess1,guess2):
points_1 = 0
points_2 = 0
if guess1 < guess2:
print ("Player 1 wins")
points_1 += 1
elif guess2 < guess1:
print ("Player 2 wins")
points_2 += 1
elif guess1 == guess2:
print ("It's a tie!")
points_1 += 1
points_2 += 1
return points_1, points_2
def start_game():
x = 0
print ("Welcome to Guess-the-Number!")
print ("Press 'q' to quit at any time.")
name1 = input("What is player one's name?: ")
name2 = input("What is player two's name?: ")
rounds = (input("How many rounds?: "))
points_1 = 0
points_2 = 0
for i in range(int(rounds)):
x += 1
secret = random.randint(0,100)
print ("Round",x)
print ("~~~~~~~")
guess1 = int(input(name1+", take a guess: "))
guess1 = get_distance(secret,guess1)
guess2 = int(input(name2+", take a guess: "))
guess2 = get_distance(secret,guess2)
new1,new2=point_machine(guess1,guess2)
points_1 += new1
points_2 += new2
print ("The number was:",secret)
print ("Score:",points_1,"to",points_2)
if points_1 > points_2:
print (name1,"wins the game!")
elif points_2 > points_1:
print (name2,"wins the game!")
elif points_1 == points_2:
print ("It's a tie!")
print (points_1,"to",points_2)
def OnKeyboardEvent(event):
if event.Key in ['q', 'Q']:
start_game()
# return True to pass the event to other handlers
return True
# create a hook manager
hm = pyHook.HookManager()
# watch for all input events
hm.KeyDown = OnKeyboardEvent
# set the hook
hm.HookKeyboard()
# wait forever
pythoncom.PumpMessages()
start_game()