【问题标题】:How can I print a string when the user presses the enter key whilst playing my guessing game?当用户在玩我的猜谜游戏时按下回车键时,如何打印字符串?
【发布时间】:2018-01-21 20:00:53
【问题描述】:

我是一名初级程序员,我创建了一个猜谜游戏,但我想合并一个字符串,当用户按下回车键时将打印该字符串,我已经尝试过,但它不起作用。 (返回错误“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!!") 

【问题讨论】:

  • guess == () 永远不会是True
  • @cᴏʟᴅsᴘᴇᴇᴅ 好吧,如果我放类似
  • @coldspeed str(guess) == ("") 这行得通吗?
  • 是的,会的。

标签: python input enter


【解决方案1】:
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")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多