Python Forum
Suggestions on my code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Suggestions on my code
#1
Ive made up this program to workout the costs of old and new movies at a video store by adding them together for some uni homework. It works and all, Just wondering if there is something that could use changing or tweaking ? Thank you in advance !!  Smile



New = 3.00
Oldie = 2.00

NewMovie = int(input("Enter the amount of new movies:"))
OldMovie = int(input("Enter the number of old movies:"))


NewAmount = New * NewMovie
OldieAmount = Oldie * OldMovie

TotalAmount = NewAmount + OldieAmount

print("The Total amount is $" + str(TotalAmount))
Reply
#2
Hello!

You can check for incorrect input. If the input is 'a hundred' instead of 100, the script will throw ValueError error message.

>>> num = int(input("A number, please: "))
A number, please: number
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'number'
invalid literal for int() with base 10: 'number'
Workaround:

# using if
num = input("Enter the amount of new movies: ")

if num.isdigit():
    amount = int(num)
else:
    print("Incorrect input. Bye!")
# using try/except

try:
    amount = int(input("Enter the amount of new movies: "))
except ValueError:
    print("Incorrect input. Bye!")
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
In addition to wavic's testing suggestions, 

Why convert "TotalAmount" to a string value?
print("The Total amount is: ${}".format(TotalAmount))
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help and suggestions jsoberano 3 2,336 May-07-2019, 03:22 PM
Last Post: michalmonday

Forum Jump:

User Panel Messages

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