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"
#1
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")
Reply
#2
Well, think about it: if g has the value 150, then is it or is it not equal to m?

Also, you really don't need to use single letter variable names most of the time - you'd be better off using more meaningful names as that helps readability.
Kakha likes this post
Reply
#3
(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")

i find way

g = 0
m = 300
while g <= m : 
    if g==m/2:
        print("We've been halfway through")
    d=m-g
    print(f"Distance traveled {g}km , left to go {d}km speed 50 km/h")
    g = g + 50
else:
    print(f"Finish")
Reply
#4
Ignore. Misread the code.
Reply
#5
(Jan-02-2021, 08:20 AM)ndc85430 Wrote: Also, d does not seem to be declared anywhere but the loop and isn't even used.

print(f"Distance traveled {g}km , left to go {d}km speed 50 km/h")
Reply
#6
I love Python I want to learn it completely
Reply
#7
Then as a next step you might think about using a progress bar. Go to Pypi.org and look at Progress 1.5 and tqdm and see if you might want to download either module.
Kakha likes this post
Reply
#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


Forum Jump:

User Panel Messages

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