Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何让Python 3.x程序允许用户控制程序的功能?_Python_Python 3.x_If Statement_Multiple Choice - Fatal编程技术网

如何让Python 3.x程序允许用户控制程序的功能?

如何让Python 3.x程序允许用户控制程序的功能?,python,python-3.x,if-statement,multiple-choice,Python,Python 3.x,If Statement,Multiple Choice,程序应该对给定的数字执行所描述的功能,但是在我给出第二个数字后,我得到了一个错误 代码 num1=int(input("Give me a number: ")) num2=int(input("Give me another number: ")) add=int(("For addition, press [1].")) time.sleep(2) sub=int(print("For subtraction, press [2].")) time.sleep(2) div=int(("F

程序应该对给定的数字执行所描述的功能,但是在我给出第二个数字后,我得到了一个错误

代码

num1=int(input("Give me a number: "))
num2=int(input("Give me another number: "))

add=int(("For addition, press [1]."))
time.sleep(2)
sub=int(print("For subtraction, press [2]."))
time.sleep(2)
div=int(("For division, press [3]."))
time.sleep(2)
multi=int(("For multiplication, press [4]."))
time.sleep(2)
print("Please input an option and press [RETURN].")

if input == 1:
    ans=num1+num2
if input == 2:
    ans=num1+num2

关于如何改进我的代码,您有什么想法吗?

如果您想获得输入替换为以下内容:

add=int(输入(“要添加,请按[1]”)

更简化的版本:

print "Basic calculator"

num1=int(input("Enter number 1: "))
num2=int(input("Enter the other number: "))

choice=int(input("For addition, press [1].\n"
    "For subtraction, press [2].\n"
    "....\n"
    "Please input the operation and press [RETURN]."))
time.sleep(2)

if choice == 1:
    ans=num1+num2
elif choice == 2:
    ans=num1-num2
    ...

print "Answer is {}".format(ans)

add=int((“要添加,请按[1])
正在尝试将字符串转换为int,然后将其存储在变量中。也许你的意思是打印,而不是int?否则,它将在将字符串转换为int时给您一个错误。另外,
如果input==1
没有引用您创建的任何变量,因此我看不出它会起作用。@RandomDavis是的,我想打印它,并使用[1][2][3][4]选项激活了代码的加法/减法/乘法/除法方面,但我无法让它工作。这不是问题的解决方案。OP的意思是打印这些行,而不是从中获取输入both@WhiteTail你只想得到一个输入并打印4行,对吗?你的意思是,你想要得到4个输入,每行一个。但是您在最后只检查了一个值,这意味着您只想得到一个输入(除了前两个数字)。此外,Abijith Mg的答案似乎是一个很好的解决方案。