当用户在玩我的猜谜游戏时按下回车键时,如何打印字符串?
How can I print a string when the user presses the enter key whilst playing my guessing game?
我是一名初级程序员,我创建了一个猜谜游戏,但我想合并一个字符串,该字符串将在用户按下回车键时打印出来,我已经试过了,但它不起作用。 (Returns 错误,"ValueError: invalid literal for int() with base 10")当用户按下回车键时。
如果用户不按回车键而只输入整数,程序运行良好。
highest = 12
lowest = 6
answer = 9
input("Press enter to play a guessing game")
guess = input("Guess a number from %d " %lowest + "to %d: " %highest)
while (int(guess) != answer) or guess == ():
if int(guess) > answer:
print ("Answer is lower")
elif guess == ():
print ("That's not valid")
else:
print("Answer is higher")
guess = input("Guess again ")
print ("Correct!!")
import random
a = int
b = int
c= int
def guessing():
guess = int(input("guess an no between 1 to 10: "))
c = random.randrange(1,11)
print("answer is",c)
if (guess == c):
print("correct guess")
elif(guess>c):
print("higher")
else:
print("lower")
d=input("\n\n Y TO START \n n to exit\n")
if (d == "y" or d == "Y"):
guessing()
elif(d =="n" or d == "N"):
print("exit")
我是一名初级程序员,我创建了一个猜谜游戏,但我想合并一个字符串,该字符串将在用户按下回车键时打印出来,我已经试过了,但它不起作用。 (Returns 错误,"ValueError: invalid literal for int() with base 10")当用户按下回车键时。
如果用户不按回车键而只输入整数,程序运行良好。
highest = 12
lowest = 6
answer = 9
input("Press enter to play a guessing game")
guess = input("Guess a number from %d " %lowest + "to %d: " %highest)
while (int(guess) != answer) or guess == ():
if int(guess) > answer:
print ("Answer is lower")
elif guess == ():
print ("That's not valid")
else:
print("Answer is higher")
guess = input("Guess again ")
print ("Correct!!")
import random
a = int
b = int
c= int
def guessing():
guess = int(input("guess an no between 1 to 10: "))
c = random.randrange(1,11)
print("answer is",c)
if (guess == c):
print("correct guess")
elif(guess>c):
print("higher")
else:
print("lower")
d=input("\n\n Y TO START \n n to exit\n")
if (d == "y" or d == "Y"):
guessing()
elif(d =="n" or d == "N"):
print("exit")