0

这是代码,

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

是的,在计算了加法之后,可打印的文本已经重复了。

4

2 回答 2

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
于 2018-06-16T13:30:31.330 回答
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.

于 2018-06-16T13:42:49.940 回答