0

I need to be able to prompt the user to enter an empty string so it can check if the answer is correct. but every time I do that I can error saying invalid literal for int() so I need to change my user_input so it can accept int() and strings(). how do I make that possible ?

# program greeting 
print("The purpose of this exercise is to enter a number of coin values") 

print("that add up to a displayed target value.\n") 

print("Enter coins values as 1-penny, 5-nickel, 10-dime,and 25-quarter.") 

print("Hit return after the last entered coin value.")

print("--------------------") 

#print("Enter coins that add up to 81 cents, one per line.")

import sgenrand

    #prompt the user to start entering coin values that add up to 81  
    while True: 
        total = 0 
        final_coin= sgenrand.randint(1,99)

        print ("Enter coins that add up to", final_coin, "cents, on per line") 

        user_input = int(input("Enter first coin: "))

        if user_input != 1 and user_input!=5 and user_input!=10 and user_input!=25:
        print("invalid input")

        else:
            total = total + user_input


        while total <= final_coin:
            user_input = int(input("Enter next coin:"))

            if user_input != 1 and user_input!=5 and user_input!=10 and user_input!=25:
                print("invalid input")                

            else:
                total = total + user_input


        if total > final_coin : 
            print("Sorry - total amount exceeds", (final_coin)) 

        elif total < final_coin:
            print("Sorry - you only entered",(total))

        else: 
            print("correct")    

        goagain= input("Try again (y/n)?:") 

        if goagain == "y":
            continue
        elif goagain == "n":
            print("Thanks for playing ... goodbye!" )
            break
user2864064
  • 81
  • 1
  • 10

1 Answers1

3
  1. Store the value returned by input() in a variable.
  2. Check that the string is not empty before calling int().
    • if it's zero, that's the empty string.
    • otherwise, try int()ing it.
Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710