Search code examples
pythoninputenter

How can I print a string when the user presses the enter key whilst playing my guessing game?


I am a beginner programmer and I've created a guessing game but I want to incorporate a string that will print when the user presses the enter key and I've tried this but it does not work. (Returns the error, "ValueError: invalid literal for int() with base 10") when the user presses enter.

The program works fine if the user does not press enter and only inputs integers.

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

Solution

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