Python IF语句和输入

Python IF语句和输入,python,Python,我只是在练习基本的python,并试图构建一个只有加法、减法和乘法函数的计算器。当我运行代码并输入1、2或3时,没有得到任何输出 这是我的密码: question1 = input("Enter 1 to add, 2 to substract, or 3 to multiply: ") if question1 == 1: num1 = input("Enter a number: ") num2 = input("Enter another number: ") re

我只是在练习基本的python,并试图构建一个只有加法、减法和乘法函数的计算器。当我运行代码并输入1、2或3时,没有得到任何输出

这是我的密码:

question1 = input("Enter 1 to add, 2 to substract, or 3 to multiply: ")
if question1 == 1:
    num1 = input("Enter a number: ")
    num2 = input("Enter another number: ")
    result = float(num1) + float(num2)
    print(result)
elif question1 == 2:
    num1 = input("Enter a number: ")
    num2 = input("Enter another number: ")
    result = float(num1) + float(num2)
    print(result)
elif question1 == 3:
    num1 = input("Enter a number: ")
    num2 = input("Enter another number: ")
    result = float(num1) + float(num2)
    print(result))

你在路上很好!如注释中所述,您的所有语句都是False,因为
input()
本身返回的字符串类型永远不会等于整数

您只需更改代码的所有
input()
,即可将其转换为要存储的int。以下是一个例子:

question1 = int(input("Enter 1 to add, 2 to substract, or 3 to multiply: "))

你在路上很好!如注释中所述,您的所有语句都是False,因为
input()
本身返回的字符串类型永远不会等于整数

您只需更改代码的所有
input()
,即可将其转换为要存储的int。以下是一个例子:

question1 = int(input("Enter 1 to add, 2 to substract, or 3 to multiply: "))

当您在Python3中使用
input()
获得一些输入时,您会得到一个字符串

用以下方法测试:

foo = input()
print(type(foo))
question1 = int(input("Enter 1 to add, 2 to substract, or 3 to multiply: "))
结果将是
(它意味着
foo
的类型是string),而不管您的输入如何。如果要将该输入用作整数,则必须将类型更改为
int
(整数)

您应该使用
int(input())
获得如下所示的整数:

foo = input()
print(type(foo))
question1 = int(input("Enter 1 to add, 2 to substract, or 3 to multiply: "))
您还必须更改每个
if else
块中的数字输入:

if question1 == 1:
    num1 = int(input("Enter a number: "))
    num2 = int(input("Enter another number: "))
    result = num1 + num2
    print(result)
或者,您可以在需要计算时更改它:

result = int(num1) + int(num2)

当您在Python3中使用
input()
获得一些输入时,您会得到一个字符串

用以下方法进行测试:

foo = input()
print(type(foo))
question1 = int(input("Enter 1 to add, 2 to substract, or 3 to multiply: "))
结果将是
(它意味着
foo
的类型是string),而不管您的输入如何。如果要将该输入用作整数,则必须将类型更改为
int
(整数)

您应该使用
int(input())
获得如下所示的整数:

foo = input()
print(type(foo))
question1 = int(input("Enter 1 to add, 2 to substract, or 3 to multiply: "))
您还必须更改每个
if else
块中的数字输入:

if question1 == 1:
    num1 = int(input("Enter a number: "))
    num2 = int(input("Enter another number: "))
    result = num1 + num2
    print(result)
或者,您可以在需要计算时更改它:

result = int(num1) + int(num2)

您需要将输入转换为整数。请尝试以下代码:

question1 = int(input("Enter 1 to add, 2 to substract, or 3 to multiply: "))
if question1 == 1:
    num1 = int(input("Enter a number: "))
    num2 = int(input("Enter another number: "))
    result = num1 + num2
    print(result)
elif question1 == 2:
    num1 = int(input("Enter a number: "))
    num2 = int(input("Enter another number: "))
    result = num1 - num2
    print(result)
elif question1 == 3:
    num1 = int(input("Enter a number: "))
    num2 = int(input("Enter another number: "))
    result = num1 * num2
    print(result)

您需要将输入转换为整数。请尝试以下代码:

question1 = int(input("Enter 1 to add, 2 to substract, or 3 to multiply: "))
if question1 == 1:
    num1 = int(input("Enter a number: "))
    num2 = int(input("Enter another number: "))
    result = num1 + num2
    print(result)
elif question1 == 2:
    num1 = int(input("Enter a number: "))
    num2 = int(input("Enter another number: "))
    result = num1 - num2
    print(result)
elif question1 == 3:
    num1 = int(input("Enter a number: "))
    num2 = int(input("Enter another number: "))
    result = num1 * num2
    print(result)
@阿德林
int(问题1)
,不是
int(“问题1”)
@Adelin
int(问题1)
,不是
int(“问题1”)