The code works fine except for the exceptions ie, when I input something like cat/dog or 1/0 or for the case 3/2, instead of re-prompting, the terminal just goes into blank infinite mode that I have to manually stop with cmd+c. Please help identify what I'm doing wrong. Thanks!
def main():
fuel=input("Fuel: ")
c=convert(fuel)
print(f"{convert(fuel)}%")
gauge(c)
def convert(fraction):
while True:
try:
X,Y=fraction.split("/")
x=int(X)
y=int(Y)
f=x/y
z=int((x/y)*100)
if f<=1:
return z
except (ValueError, ZeroDivisionError):
pass
def gauge(percentage):
if percentage<=1:
print("E")
elif percentage>=99:
print("F")
else:
print("Z%")
if __name__ == "__main__":
main()
Well, you never re-ask the question. To solve it, move the input("Fuel: ")
call inside the while
loop:
def main():
c = convert()
print(f"{c}%")
gauge(c)
def convert():
while True:
fraction = input("Fuel:")
try: # Rest can be the same
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.