初学者python“无”问题

时间:2018-06-24 16:38:19

标签: python

我刚开始使用python,自从我启动了一个新的计算器项目以来,pyCharm便一无所获。我不确定导致此错误的原因,如果能在这里获得帮助,我将不胜感激。 (这只是我要显示的主要功能)这是代码:

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

发生的情况的示例:

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

3 个答案:

答案 0 :(得分:4)

print个调用中删除input条语句:

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

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

答案 1 :(得分:1)

尝试删除输入中的打印语句。
代替input(print("Would you like to continue, yes or no?")) ,请尝试
input("Would you like to continue, yes or no?")
那就是我要做的

答案 2 :(得分:1)

用于输入的函数签名为lines = [x for x in dates.split('\n')] new_list = [x.replace("2017-04-13 ", '') for x in lines] 。括号表示提示是可选的,但基本上input([prompt])要求您提供要打印的字符串。相反,您要给它一个input语句。打印语句的返回值为print(),因此输入语句会将其作为提示输出。

请注意,使用None语句而不指定错误类型也是一种不好的做法。在不指定类型的情况下,except将触发任何错误(例如,当我尝试在添加except之类的函数之前运行代码时触发该错误)。我认为您想在这里multi