第二个循环,游戏无法运行和 Sublime Text 问题
Second loop, game not working and Sublime Text problems
我正在开发一个非常简单的 'game',玩家可以通过 5 次猜测来猜测一个随机数。
还没有完成,但我 运行 遇到了几个问题。
这是生成随机数并允许玩家猜测的代码
相关代码:
def GuessLoopFunc(guess):
import random
import sys
import time
sum_guesses = 0
rand_num = random.randrange(1, 21)
print(rand_num)
#This is just for test purposes to know the correct number and see what happens when I guess the correct number
while True:
if guess == rand_num:
print("You got it right!")
else:
sum_guesses += 1
if sum_guesses == 4:
guess = input("That's incorrect...final guess: ")
continue
elif sum_guesses == 5:
print("Oh no! You lost!")
while True:
replay = input("Do you want to play again: ")
if replay == "yes" or replay == "Yes":
pass #Temporary while I figure out how to loop back to very start (new random number)
elif replay == "no" or replay == "No":
print("Goodbye")
break
else:
print("I do not understand what you mean...")
continue
else:
guess = input("You got it wrong, guess again: ")
continue
正如您在我发表的评论中看到的那样,如果玩家表示他们想再次玩游戏(这样他们会得到一个新的随机数),我希望游戏 return 进入程序的最开始。
此外,由于某种原因,游戏在给出正确答案时没有注册,并不断告诉玩家他的答案不正确...这是调用上述模块的游戏代码:
import sys
import random
import time
from GuessLoop import GuessLoopFunc
print("Hello! Welcome to the guess the number game!")
name_player = input("Please enter your name: ")
print("Hello " + str(name_player) + "!")
play = input("Are you ready to play? ")
if play == "Yes" or play == "yes":
print("Great! Let's get started...")
elif play == "No" or play == "no":
print("Too bad...")
sys.exit()
else:
print("I do not understand your response...")
quit1 = input("Do you want to quit? ")
if quit1 == "yes" or quit1 == "Yes":
sys.exit()
elif quit1 == "no" or quit1 == "No":
print("Great! Let's get started!")
else:
print("I do not understand your response...quitting.")
sys.exit()
print("I am going to think of think of a number between 1 and 20...")
print("You will get 5 guesses to guess the number...")
time.sleep(1)
print("Okay, I have a number in mind")
guess = input("What is your guess? ")
GuessLoopFunc(guess)
time.sleep(1)
sys.exit()
最后,当我尝试 运行 Sublime Text 中的程序时,它 运行 没有比“请输入您的姓名:”部分更进一步。如果我填写我的名字并按回车键,没有任何反应......但也没有显示错误消息。所以我每次都求助于在 Python IDLE 中测试程序,但这有点乏味......任何人都知道发生了什么。
您的主要问题是您将用户输入 (a string
) 与随机数 (integer
) 进行比较 - 它们 永远不会相同 作为 string != int
.
解法:
您需要通过 int(text) 函数将用户输入转换为数字。
def getInt(text):
while True:
try:
n = input(text)
return int(n)
except ValueError: # if the input is not a number try again
print(f"{n} is not a number! Input a number")
....
guess = getInt("What is your guess?") # now that is an int
....
您有很多可以简化的重复“yes/no”代码部分:
def YesNo(text):
'''Ask 'test', returns True if 'yes' was answerd else False'''
while True:
answer = input(text).strip().lower()
if answer not in {"yes","no"}:
print("Please answer 'yes' or 'no'")
continue # loop until yes or no was answered
return answer == "yes"
这减少了
quit1 = input("Do you want to quit? ")
if quit1 == "yes" or quit1 == "Yes":
sys.exit()
elif quit1 == "no" or quit1 == "No":
print("Great! Let's get started!")
至
if YesNo("Do you want to quit? "):
sys.exit()
else:
pass # do somthing
并在所有 yes/no 个问题中使用它。
要再玩一次,我会将“你想再玩一次吗”问题移出游戏循环:
# do your name questioning and greeting here
# and then enter an endless loop that you break from if not playing again
while True:
GuessLoopFunc() # move the guess input inside the GuessLoopFunk as it has
# to be done 5 times anyhow. If you spend 5 guesses or
# guessed correctly, print some message and return to here
if YesNo("Play again? "):
continue
else:
break # or sys.exit()
解决 sublime 问题:Issue with Sublime Text 3's build system - can't get input from running program
我正在开发一个非常简单的 'game',玩家可以通过 5 次猜测来猜测一个随机数。
还没有完成,但我 运行 遇到了几个问题。
这是生成随机数并允许玩家猜测的代码
相关代码:
def GuessLoopFunc(guess):
import random
import sys
import time
sum_guesses = 0
rand_num = random.randrange(1, 21)
print(rand_num)
#This is just for test purposes to know the correct number and see what happens when I guess the correct number
while True:
if guess == rand_num:
print("You got it right!")
else:
sum_guesses += 1
if sum_guesses == 4:
guess = input("That's incorrect...final guess: ")
continue
elif sum_guesses == 5:
print("Oh no! You lost!")
while True:
replay = input("Do you want to play again: ")
if replay == "yes" or replay == "Yes":
pass #Temporary while I figure out how to loop back to very start (new random number)
elif replay == "no" or replay == "No":
print("Goodbye")
break
else:
print("I do not understand what you mean...")
continue
else:
guess = input("You got it wrong, guess again: ")
continue
正如您在我发表的评论中看到的那样,如果玩家表示他们想再次玩游戏(这样他们会得到一个新的随机数),我希望游戏 return 进入程序的最开始。
此外,由于某种原因,游戏在给出正确答案时没有注册,并不断告诉玩家他的答案不正确...这是调用上述模块的游戏代码:
import sys
import random
import time
from GuessLoop import GuessLoopFunc
print("Hello! Welcome to the guess the number game!")
name_player = input("Please enter your name: ")
print("Hello " + str(name_player) + "!")
play = input("Are you ready to play? ")
if play == "Yes" or play == "yes":
print("Great! Let's get started...")
elif play == "No" or play == "no":
print("Too bad...")
sys.exit()
else:
print("I do not understand your response...")
quit1 = input("Do you want to quit? ")
if quit1 == "yes" or quit1 == "Yes":
sys.exit()
elif quit1 == "no" or quit1 == "No":
print("Great! Let's get started!")
else:
print("I do not understand your response...quitting.")
sys.exit()
print("I am going to think of think of a number between 1 and 20...")
print("You will get 5 guesses to guess the number...")
time.sleep(1)
print("Okay, I have a number in mind")
guess = input("What is your guess? ")
GuessLoopFunc(guess)
time.sleep(1)
sys.exit()
最后,当我尝试 运行 Sublime Text 中的程序时,它 运行 没有比“请输入您的姓名:”部分更进一步。如果我填写我的名字并按回车键,没有任何反应......但也没有显示错误消息。所以我每次都求助于在 Python IDLE 中测试程序,但这有点乏味......任何人都知道发生了什么。
您的主要问题是您将用户输入 (a string
) 与随机数 (integer
) 进行比较 - 它们 永远不会相同 作为 string != int
.
解法:
您需要通过 int(text) 函数将用户输入转换为数字。
def getInt(text):
while True:
try:
n = input(text)
return int(n)
except ValueError: # if the input is not a number try again
print(f"{n} is not a number! Input a number")
....
guess = getInt("What is your guess?") # now that is an int
....
您有很多可以简化的重复“yes/no”代码部分:
def YesNo(text):
'''Ask 'test', returns True if 'yes' was answerd else False'''
while True:
answer = input(text).strip().lower()
if answer not in {"yes","no"}:
print("Please answer 'yes' or 'no'")
continue # loop until yes or no was answered
return answer == "yes"
这减少了
quit1 = input("Do you want to quit? ") if quit1 == "yes" or quit1 == "Yes": sys.exit() elif quit1 == "no" or quit1 == "No": print("Great! Let's get started!")
至
if YesNo("Do you want to quit? "):
sys.exit()
else:
pass # do somthing
并在所有 yes/no 个问题中使用它。
要再玩一次,我会将“你想再玩一次吗”问题移出游戏循环:
# do your name questioning and greeting here
# and then enter an endless loop that you break from if not playing again
while True:
GuessLoopFunc() # move the guess input inside the GuessLoopFunk as it has
# to be done 5 times anyhow. If you spend 5 guesses or
# guessed correctly, print some message and return to here
if YesNo("Play again? "):
continue
else:
break # or sys.exit()
解决 sublime 问题:Issue with Sublime Text 3's build system - can't get input from running program