Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Program does not end
#1
Hello,

I'm working on a homework assignment and I'm having issues with the program not ending after an option other than "1" is selected. When I select option 2, the program prints "High card is coming soon" but continues to prompt for an amount instead of ending. It happens on options 3 as well as the invalid options. I'm also having an issue where if the cost is more than the payment, it gives the appropriate response "You did not pay enough." but then gives a traceback error and tries to continue instead of terminating.

Here's the assignment:

Then your program will make a decision to do something based on the option:

If the user chooses option 1, your program will play the Make Change game
If the user chooses option 2, High Card, your program will display the message “High Card is coming soon.”
If the user chooses option 3, your program will do nothing - your program should just end naturally.
If the user chooses an invalid option (option < 1 or option > 3) your program should display the message “Invalid option” and terminate naturally. Do not try to re-prompt for input.

The Make Change game does the following:
Allow a clerk to enter the amount of money a customer owes for a purchase and the amount of money the customer paid.
If the user did not pay enough, just display a message telling them they did not pay enough - do not try to ask for any more money.
If they paid enough, the game should calculate and display the amount of change, then break it down into the number of dollars, quarters, dimes, nickels and pennies to return to the customer.


Here's my code:

# Display game menu
print("Menu")
print("1. Make Change")
print("2. High Card")
print("3. Quit")
print( )

# Enter option
option=int(input("Enter a number to choose an \
option: "))

# Decision display
if option == 1:
    print( )
    print('Make Change Game')
elif option == 2:
    print( )
    print('High Card is coming soon.')
elif option == 3:
    print ()
elif option < 1 or option > 3:
    print( )
    print('Invalid option')

# Define payment and cost
payment = float(input("Enter the payment amount: "))
cost = float(input("Enter the cost: "))

if payment < cost:
    print('You did not pay enough.')
else:
    change = payment - cost

# Display the change due
print('\nchange due: $',format(change, '.2f'))

# Calulate number of dollars
dollars = int(change//1)
print('Dollars:',dollars)

# Convert change
change = int(((change-dollars) + .005)* 100)

# Calculate number of quarters
quarters = change // 25
print('Quarters:',quarters)
change = change - (quarters * 25)

# Calculate number of dimes
dimes = change // 10
print('Dimes:',dimes)
change = change - (dimes * 10)

# Calculate number of nickels
nickels = change // 5
print('Nickels:',nickels)
change = change - (nickels * 5)

# Pennies due
pennies = change
print('Pennies:',pennies)
Any help is appreciated.
Thank you!
Reply
#2
Please use code tags. We can't see the indentation in your code, which can be crucial to solving problems like this. See the BB Code link in my signature for details on how to do that.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Yeah, so look at it this way. You asked for an option, the user gave that, you printed out the corresponding text. All done, and done well.
The program continues because you haven't given a condition for when to do the rest. It is just there after this interaction with the user. The Make Change lines should run only if the user selects 1.
So put that whole code inside your 'if option == 1' block.

A similar issue happens after you enter the Make Change game. Say the customer did not pay enough, you print out 'You did not pay enough'. But then the rest still executes. You should put the rest of the game code in the else block.

When writing code, always try and understand under what condition will your code run, and if it is running only when you want to.
Reply
#4
(Jun-18-2018, 08:11 AM)cryomick Wrote: Yeah, so look at it this way. You asked for an option, the user gave that, you printed out the corresponding text. All done, and done well.
The program continues because you haven't given a condition for when to do the rest. It is just there after this interaction with the user. The Make Change lines should run only if the user selects 1.
So put that whole code inside your 'if option == 1' block.

A similar issue happens after you enter the Make Change game. Say the customer did not pay enough, you print out 'You did not pay enough'. But then the rest still executes. You should put the rest of the game code in the else block.

When writing code, always try and understand under what condition will your code run, and if it is running only when you want to.

Thanks Cryomick, that was it. Also I didn't have the indentions formatted correctly but it's running as it should now.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020