In this section, you are primarily going to be working with Functions, User input (with the input()
function) ints, and even some if-statements).
Problem 1
Create a new python program file and name it time.py .
This program will convert weeks to hours.
When you first run this program, you should ask the user (with the `input()` function) for one value - the number of weeks.
For now, don't worry about error-checking or input validation.
After this, print out the conversion of this amount of weeks to hours.
Here's a simple example of the program running:
How many weeks? 3
There are 504 hours in 3 weeks.
|
Problem 2
Next, modify time.py to validate the input.
The program should only accept integer numbers as input from the user.
To do this, you'll need the isnumeric() function and an if-else statement.
If the user types in a valis number, the program should work like before:
How many weeks? 4
There are 672 hours in 4 weeks.
If the input is not a integer, the program should print an error message, and should _not_ attempt to do the computation.
How many weeks? BLAH
Sorry, I can't compute that.
|
Problem 3
Next, we'll add another feature to our program.
This time, we will modify time.py to allow the user to conver either weeks or months to hours.
This will require an additional input from the user.
Now running the program will look like these examples:
Months or weeks? months
How many months? 2
There are 1440 hours in 2 months.
Months or weeks? weeks
How many weeks? 2
There are 336 hours in 2 weeks.
In order to do this, you'll need to use an if-else statement to check wether or not the first input is "week" or "month".
Assuming you store the first input into a variable named unit , this would look like this:
if (unit == "month"):
Put code here to do month computation
elif (unit == "week"):
Put code here to to week computation
else:
Print a warning message
|
Problem 4
Yet another modification of time.py .
Add the ability for the user to provide more options: days, weeks, months, years.
Now, the user can choose to convert 4 different types of units into hours.
This will require adding-on to the if-statement you write earlier.
|
If you complete all of the problems with time remaining, work on the assignment!
You must get to at least problem 3 of the 4 problem components.
If it isn’t working perfectly before you turn it in, that’s OK.
We aren’t looking for perfection.
Solutions: