Python Forum
please help i cant print "We've been halfway through"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
please help i cant print "We've been halfway through"
#8
(Jan-02-2021, 07:57 AM)Kakha Wrote:
g = 0
m = 300
while g <= m : 
    if g!=m :
        d=m-g
    elif g==m/2:
        print("We've been halfway through")
    print(f"Distance traveled {g}km , left to go {d}km speed 50 km/h")
    g = g + 50
else:
    print(f"Finish")

Your code says if g doesn't equal m. So it's going to check for that first. Since g is not equal to m, that if statement runs. Then you have an elif statement, which means else if. So basically if your first statement isnt true, check this statement. Since your first statement keeps being true, your elif doesn't run. You can fix this by changing elif to if. Then you have to statemens and the second statement doesn't rely on your first if statement.
g = 0
m = 300
while g <= m : 
    if g!=m :
        d=m-g
    if g==m/2:
        print("We've been halfway through")
    print(f"Distance traveled {g}km , left to go {d}km speed 50 km/h")
    g = g + 50
else:
    print(f"Finish")
Reply


Messages In This Thread
RE: please help i cant print "We've been halfway through" - by blacksword - Jan-05-2021, 08:24 PM

Forum Jump:

User Panel Messages

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