当用户在玩猜谜游戏时按回车键时,如何打印字符串?

2024-04-23 10:58:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一个初学者程序员,我创造了一个猜谜游戏,但我想纳入一个字符串,将打印时,用户按回车键,我已经尝试了这个,但它不起作用。(当用户按enter键时,返回错误“ValueError:invalid literal for int(),以10为基数)。你知道吗

如果用户不按enter键,只输入整数,程序就可以正常工作。你知道吗

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!!") 

Tags: to用户answerinputis程序员intprint
1条回答
网友
1楼 · 发布于 2024-04-23 10:58:02
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")

相关问题 更多 >