0
Help i am unable to run my code in code playground...
while True: print("add for addition") print("sub for substraction") print("mul for multiplication") print("div for division") userin = input(": ") x = float(input("a:")) y = float(input("b:")) resultadd = str(x + y) resultsub = str(x - y) resultmul = str(x * y) resultdiv = str(x / y) if userin == "add": print("result is " + resultadd) elif userin == "sub": print("result is " + resultsub) elif userin == "mul": print("result is " + resultmul) elif userin == "div":
2 Answers
+ 5
# You need a 'break' statement to terminate your while loop as soon as the result is printed, otherwise it would continue taking input and printing the text forever.
while True:
print("add for addition")
print("sub for substraction")
print("mul for multiplication")
print("div for division")
userin = input(": ")
x = float(input("a:"))
y = float(input("b:"))
resultadd = str(x + y)
resultsub = str(x - y)
resultmul = str(x * y)
resultdiv = str(x / y)
if userin == "add":
print("result is " + resultadd)
break
elif userin == "sub":
print("result is " + resultsub)
break
elif userin == "mul":
print("result is " + resultmul)
break
elif userin == "div":
print("result is " + resultdiv)
break
+ 1
Alternatively, you could add a single break after all then conditional statements.