简单:Python 要求输入两次 [英] Simple:Python asks for input twice

查看:67
本文介绍了简单:Python 要求输入两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

def calculator(value1,value2):

    function=input("Function?")
    if function=="*":
     return value1*value2
    if function=="/":
     return value1/value2
    if function=="+":
     return value1+value2
    if function=="-":
     return value1-value2
a=float(input("value 1:"))
b=float(input("value 2:"))
calculator(a,b)
print(calculator(a,b))

Python Shell 上的输出

Output on Python Shell

value 1:5
value 2:5
Function?/
Function?/
1.0

所以我只是想知道为什么它要求输入函数两次,而不是一次.这可能是一个愚蠢的问题,但感谢您的回答.

So im just wondering why it asks for input for function twice, not once. This is probably a stupid question but thanks for answering.

推荐答案

这两行导致了您的问题:

These two lines are causing your problem:

calculator(a,b)
print(calculator(a,b))

您调用了 calculator 两次,因此它要求您输入两次.

You're calling calculator twice, so it's asking you for input twice.

要修复您的代码,只需将 calculator(a, b) 的结果存储在一个变量中,然后将其打印出来:

To fix your code, just store the result of calculator(a, b) in a variable and then print it out:

result = calculator(a, b)
print(result)

这篇关于简单:Python 要求输入两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆