Python Forum
Problem with my If, Elif, and Else code.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with my If, Elif, and Else code.
#1
Hey what's up. I have a problem with a question of mine.

I wrote these codes to program how many seconds are there since midnight. I'll type in what I have codded below. And I try to run the code, it is z little sloppy but still I thought it could work. When I run the program I type in my Hours, Minutes, and Seconds. Then I listed t and t2 as the equation for the inputs. I stated on the variable a == ether 'am' or 'pm'. and typing in the t for am and t2 for pm. When I do that all it does is copies am or pm that I typed. I am new to programming and I need some explanation on why this isn't the right.



print('Hello, this is a program to calculate seconds since midnight.')

print('What is the Hour now')
h=(input(int))

print('What is the Minute now')
m=(input(int))

print('What is the Second now')
s=(input(int))

t = ((3600 * h) + (m * 60 + s))

t2 = ((3600 * h) + (m * 60 + s) * 2)

a = print((input('Is it am or pm')))
if a == 'am':
    print('Your answer is, ', t, 'seconds since midnight')
elif a =='pm':
    print('Your answer is, ', t2, 'seconds since midnight')
else:
    "You have typed in the wrong inputs, try again."
Reply
#2
The result of the print() at line 16 is not what you think. A call to print() returns the None value.
Reply
#3
(Sep-02-2020, 05:26 PM)Gribouillis Wrote: The result of the print() at line 16 is not what you think. A call to print() returns the None value.

I see, So I deleted the print() from line 16, but now when I type in my numbers for the time I get 1's repeating. Is there something wrong with t and t2? and how do I fix that. Here is the code and I'll give you the details I used to start the program.

print('Hello, this is a program to calculate seconds since midnight.')

print('What is the Hour now')
h=(input(int))

print('What is the Minute now')
m=(input(int))

print('What is the Second now')
s=(input(int))

t = ((3600 * h) + (m * 60 + s))

t2 = ((3600 * h) + (m * 60 + s) * 2)

a = (input('Is it am or pm'))
if a == 'am':
    print('Your answer is, ', t, 'seconds since midnight')
elif a =='pm':
    print('Your answer is, ', t2, 'seconds since midnight')
else:
    "You have typed in the wrong inputs, try again."
* Write a program to calculate the number of seconds since midnight. For example, suppose the time is 1:02:05 AM. Since there are 3600 seconds per hour and 60 seconds per minutes, it has been 3725 seconds since midnight (3600 * 1 + 2 * 60 + 5 = 3725). The program asks the user to enter 4 pieces of information: hour, minute, second, and AM/PM. The program will calculate and display the number of seconds since midnight. [Hint: be very careful when the hour is 12]. *

do I just make the t = ((3600 * h) + (m * 60 + s)), and t2 = ((3600 * h) + (m * 60 + s) * 2) and put them all in 1 ()'a sort of like this

t2 = (3600 * h + m * 60 + s * 2)
Reply
#4
h=(input(int))
The argument passed to input() (the bit inside the parenthesis) is what is shown to you on screen to prompt the input. You've printed your prompt in a separate statement. By putting int in there, it prints the string representation of the int object. So it prints
Output:
<class 'int'>


What you're not doing is you're not converting it to an int, but leaving it as a string. Then instead of doing numeric math, it interprets the * as string mulitplication. So you get 3600 copies of the hour.

You need the int function (which has parenthesis) to convert a string to an int.
numeric_hours = int(string_hours)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Whats the right way to refactor this Big if/elif/elif ? pitosalas 1 875 Jul-28-2019, 05:52 PM
Last Post: ichabod801
  Problem with elif statement Haddal99 2 994 May-20-2019, 09:26 AM
Last Post: avorane
  if elif problem :) Runer 2 1,239 Apr-22-2019, 01:57 PM
Last Post: Runer
  Problem witrh else and elif values. anolibal 7 4,131 Aug-20-2018, 11:50 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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