这是代码,
while True :
print(" your options are ")
print(" enter add to add numbers ")
print(" enter sub to subtract two numbers ")
user_input = input("")
if user_input == "quit":
break
elif user_input == "add":
print ("you entered" +user_input )
num1 = float (input ("enter a number"))
num2 = float (input ("enter second number"))
res = str ( num1 + num2 )
print ( "result is" +res)
elif user_input == "sub":
print (user_input)
num3 = float (input ("enter a number"))
num4 = float (input ("enter second number"))
res1 = str ( num3 + num4 )
print ( "result is" +res4)
并输入:
add
1
1
这是输出
your options are
enter add to add numbers
enter sub to subtract two numbers
you enteredadd
enter a number enter second number result
is2.0
your options are
enter add to add numbers
enter sub to subtract two numbers
Traceback (most recent call last):
File "..\Playground", line 5, in
<module>
user input = input("")
EOFError: EoF when reading a line
是的,在计算了添加后,重复了可打印文本。
答案 0 :(得分:0)
计算器在打印完操作结果后再次启动,因为你没有break
退出你的while循环。
请注意,您的最后一行还有一个未定义的名称ref4
。你可能意味着res1
。
while True:
print(" your options are ")
print(" enter add to add numbers ")
print(" enter sub to subtract two numbers ")
user_input = input("")
if user_input == "quit":
break
elif user_input == "add":
print("you entered" + user_input)
num1 = float(input("enter a number"))
num2 = float(input("enter second number"))
res = str(num1 + num2)
print("result is" + res)
break # This statement was added
elif user_input == "sub":
print(user_input)
num3 = float(input("enter a number"))
num4 = float(input("enter second number"))
res1 = str(num3 + num4)
print("result is" + res1) # 'res4' was replaced by 'res1'
break # This statement was added
your options are
enter add to add numbers
enter sub to subtract two numbers
add
you entered add
enter a number 1
enter second number 2
result is 3.0
尽管如此,让我使用operator
模块建议一个改进的,更具可扩展性的版本。
import operator
operations = {
'add': operator.add,
'sub': operator.sub,
'mul': operator.mul
}
while True:
print('Your options are: ', 'quit', *operations)
op = input('Enter an operation: ')
if op == 'quit':
break
else:
try:
op = operations[op]
except KeyError:
print('Invalid opeartion')
continue
x = float(input('Enter a number: '))
y = float(input('Enter a number: '))
print('The result is:', op(x, y))
break
Your options are: quit add sub mul
Enter an operation: mul
Enter a number: 2
Enter a number: 4
The result is: 8.0
答案 1 :(得分:0)
我还认为您的代码还有其他错误。 例如
res1 = str ( num3 + num4 )
print ( "result is" +res4)
它应该解决这个问题:
res1 = num3 - num4
print ( "result is %f"%(res1))
这是我的代码:
while True :
print(" your options are ")
print(" enter add to add numbers ")
print(" enter sub to subtract two numbers ")
user_input = input("")
if user_input == "quit":
break
elif user_input == "add":
print ("you entered" +user_input )
num1 = float (input ("enter a number"))
num2 = float (input ("enter second number"))
res = num1 + num2
print ( "result is %f" %(res))
break
elif user_input == "sub":
print (user_input)
num3 = float (input ("enter a number"))
num4 = float (input ("enter second number"))
res1 = num3 - num4
print ( "result is %f"%(res1))
break
输出:
your options are
enter add to add numbers
enter sub to subtract two numbers
sub
sub
enter a number-1.7
enter second number-8.7
result is 7.000000
如果删除break
:
while True :
print(" your options are ")
print(" enter add to add numbers ")
print(" enter sub to subtract two numbers ")
user_input = input("")
if user_input == "quit":
break
elif user_input == "add":
print ("you entered" +user_input )
num1 = float (input ("enter a number"))
num2 = float (input ("enter second number"))
res = num1 + num2
print ( "result is %f" %(res))
elif user_input == "sub":
print (user_input)
num3 = float (input ("enter a number"))
num4 = float (input ("enter second number"))
res1 = num3 - num4
print ( "result is %f"%(res1))
输出:
your options are
enter add to add numbers
enter sub to subtract two numbers
add
you enteredadd
enter a number4
enter second number9
result is 13.000000
your options are
enter add to add numbers
enter sub to subtract two numbers
sub
sub
enter a number4
enter second number5
result is -1.000000
your options are
enter add to add numbers
enter sub to subtract two numbers
quit
while true
让程序运行,直到您输入quit
。