首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >初学者python“无”问题

初学者python“无”问题
EN

Stack Overflow用户
提问于 2018-06-25 00:38:19
回答 3查看 407关注 0票数 2

我刚刚开始使用python,因为我开始了一个新的计算器项目,pyCharm在一切之后都不会吐出任何东西。我不确定是什么导致了这个错误,如果我能在这里得到一些帮助,我将不胜感激。(这只是我展示的主函数)下面是代码:

代码语言:javascript
复制
def main():
    run = True
    while run == True:
        if run == False:
        break
    try:
        operation = input(print("Would you like to *, -, + or /?"))

        if operation != "+" and operation != "-" and operation != "/" and operation != "*":
            print("invalid input.")
            go = input(print("Would you like to continue, yes or no?"))

        if go == "no":
            run = False

        else:
            continue

        else:
            num1 = int(input(print("What's your first number?")))

            num2 = int(input(print("What's your second number?")))

         if operation == "*":
            print(multi(num1, num2))

         if operation == "-":
            print(sub(num1, num2))

         if operation == "+":
               print(add(num1, num2))

         if operation == "/":
               print(div(num1, num2))

         go = input(print("Would you like to make another calculation, yes or no?"))
         if go == "no":
                run = False
                else:
                    continue

        except:
            print("invalid input.")
            go = input(print("Would you like to continue, yes or no?"))
            if go == "no":
                run = False
            else:
               continue

下面是一个发生情况的示例:

代码语言:javascript
复制
Would you like to *, -, + or /?
None/
What's your first number?
None3
What's your second number?
None4
0.75
Would you like to make another calculation, yes or no?
Noneno

Process finished with exit code 0
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-06-25 00:57:42

输入的函数签名为input([prompt])。括号表示提示符是可选的,但基本上input希望您提供一个字符串以打印出来。相反,您将为其提供一条print()语句。打印语句的返回值是None,因此它将作为input语句的提示符输出。

请注意,使用except语句而不指定错误类型也是不好的做法。在没有指定类型的情况下,except将在出现任何错误时触发(例如,当我尝试在添加multi等函数之前运行您的代码时触发)。我觉得你想让except ValueError留在这里。

票数 1
EN

Stack Overflow用户

发布于 2018-06-25 00:43:28

input调用中删除print语句:

input(print("What's your first number?")) -> input("What's your first number?\n")input("Your first number: ")

Noneprint函数的返回值,由input函数显示。

票数 4
EN

Stack Overflow用户

发布于 2018-06-25 00:44:06

尝试删除输入中的print语句。

而不是input(print("Would you like to continue, yes or no?")),请尝试

input("Would you like to continue, yes or no?")

这就是我要做的

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51011912

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档