0
def change():
    if choice == 1:    #<--- issue here
        while True:
            amt = float(input("Enter amount of cash: $"))
            if amt >= a:
                print("Your change will be: ${:.2f}".format(amt - a))
                break
            else:
                print("Not enough, missing: ${:.2f}".format(a - amt))
                input("press enter to continue")


a = 15.60
b = 56.12
c = 89.53
d = 32.93
print("1. a: $15.60\t\t\t2. b: $56.12\n3. c: $89.53\t\t\t4. d: $32.93")
choice = input("Choose product (1-4): ")
change()

If I remove line 2, it would function properly but choice 1 would not be selected. I'd like it so this would run while choice 1 is selected. For some reason it's not allowing me to put an if statement before while loop. Is there a solution?

4
  • 1
    In Python 3.x, input() produces strings. No string is equal to 1. You either need to apply int() to the input, or compare it against "1". Commented Mar 3, 2019 at 5:19
  • 1
    choice = int(input("Choose product (1-4): ")). Commented Mar 3, 2019 at 5:19
  • Ahh I forgot! Thank you guys! Commented Mar 3, 2019 at 5:22
  • The problem has nothing to do with the while statement. You would have the same problem with any statement there. Commented Mar 3, 2019 at 5:22

3 Answers 3

2

It's the problem in your input statement. In python 3 input get default as string.So you need convert it to integer as below.

choice = int(input("Choose product (1-4): "))
Sign up to request clarification or add additional context in comments.

Comments

1

Cast String to integer in python 3.x use int()

choice = int(input("Choose product (1-4): "))

Comments

0

Please try below code

def change():
print(choice, choice == 1, type(choice), int(choice) == 1)
if int(choice) == 1:    #<--- issue here
    while True:
        amt = float(input("Enter amount of cash: $"))
        if amt >= a:
            print("Your change will be: ${:.2f}".format(amt - a))
            break
        else:
            print("Not enough, missing: ${:.2f}".format(a - amt))
            input("press enter to continue")


a = 15.60
b = 56.12
c = 89.53
d = 32.93
print("1. a: $15.60\t\t\t2. b: $56.12\n3. c: $89.53\t\t\t4. d: $32.93")
choice = input("Choose product (1-4): ")
change()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.