如何将用户输入解析为数字

时间:2014-01-21 15:46:14

标签: python python-3.x

我正在按照教程在Python中制作一个计算器程序。这是我的代码:

print ("This is a calculator program, press Enter to continue")
a = input()

while a == "":
    print("Enter 1 for option 1 which adds")
    print("Enter 2 for option 2 which subtracts")
    print("Enter 3 for option 3 which multiply")
    print("Enter 4 for option 4 which divides")
    print("Enter 5 for option 5 which quits",)

    Option = input("Enter an option number:")
    int(Option)

    if Option == 1:
        Number1 = input("Enter number 1")
        Number2 = input("Enter number 2")
        int(Number1,Number2)
        print(Result = Number1 + Number2)

    if Option == 2:
        Number1 = input("Enter number 1")
        Number2 = input("Enter number 2")
        int(Number1,Number2)
        print(Result = Number1 - Number2)

    if Option == 3:
        Number1 = input("Enter number 1")
        Number2 = input("Enter number 2")
        int(Number1,Number2)
        print(Result = Number1 * Number2)

    if Option == 4:
        Number1 = input("Enter number 1")
        Number2 = input("Enter number 2")
        int(Number1,Number2)
        print(Result = Number1 / Number2)

    if Option == 5:
        break

这是非常基本的,它可以打印所有选项号,然后让我选择一个。所以我输入“1”作为字符串,将其解析为整数1.然而,它不直接进入选项1而是再次循环,这很好我稍后会对其进行排序。但是当我输入1-5时,它再也没有任何选择。我想我输入了错误的代码来解析它或什么?

5 个答案:

答案 0 :(得分:0)

input函数的结果是string,您需要使用int类型将其转换为int

>>> foo = "3"
>>> foo
'3'
>>> int(foo)
3

你的误解可能来自于python是一种动态类型的语言。但请记住,尽管变量本身是无类型的,但变量值有类型。

>>> type(foo)
<class 'str'>
>>> type(int(foo))
<class 'int'>

答案 1 :(得分:0)

您的代码应该更像这样:

print("This is a calculator program. Press Enter to continue.")

while True:
    _ = input()
    print("Enter 1 for option 1 which adds")
    print("Enter 2 for option 2 which subtracts")
    print("Enter 3 for option 3 which multiply")
    print("Enter 4 for option 4 which divides")
    print("Enter 5 for option 5 which quits")

    option = int(input("Enter an option number: "))

    if option == 5:
        break
    else:
        number1 = int(input("Enter number 1: "))
        number2 = int(input("Enter number 2: "))
        if option == 1:
            result = number1 + number2
        elif option == 2:
            result = number1 - number2
        elif option == 3:
            result = number1 * number2
        elif option == 4:
            result = number1 / number2
        print(result)

突出点:

  • 您没有对a做任何事情。所以我摆脱它,并调用input将其结果存储在_中,这是一个你不关心它的值的变量的标准名称。
  • 您必须明确将option转换为int。 Python不会为您隐式转换,因此'1' != 1
  • 您无法就地转换为int - 写作int(number1)不会做任何事情。您必须写number1 = int(number1)或类似内容。
  • 您无法在int形式的单个语句中将多个字符串转换为int(number1, number2)。你在这里真正做的是调用int(x, base),将x转换为int,解释为基于base
  • 我重构了您的if陈述以使其更简洁
  • 变量名在Python中通常是小写的。
  • 您无法在print语句中分配变量。

答案 2 :(得分:0)

发布的代码包含多个错误,下面是更正后的代码:

print ("This is a calculator program, press Enter to continue")
a = input()

while a == "":
    print("Enter 1 for option 1 which adds")
    print("Enter 2 for option 2 which subtracts")
    print("Enter 3 for option 3 which multiply")
    print("Enter 4 for option 4 which divides")
    print("Enter 5 for option 5 which quits",)

    Option = int(input("Enter an option number:"))

    if Option == 1:
        Number1 = int(input("Enter number 1"))
        Number2 = int(input("Enter number 2"))
        # int(Number1,Number2)
        Result = Number1 + Number2

    if Option == 2:
        Number1 = int(input("Enter number 1"))
        Number2 = int(input("Enter number 2"))
        # int(Number1,Number2)
        Result = Number1 - Number2

    if Option == 3:
        Number1 = int(input("Enter number 1"))
        Number2 = int(input("Enter number 2"))
        # int(Number1,Number2)
        Result = Number1 * Number2

    if Option == 4:
        Number1 = int(input("Enter number 1"))
        Number2 = int(input("Enter number 2"))
        # int(Number1,Number2)
        Result = Number1 / Number2

    print(Result)

    if Option == 5:
        break

答案 3 :(得分:0)

input()将输入转换为字符串,因此如果您需要阅读int,则必须将其投射。

if条件下,您可以将input()结果(字符串)转换为int

Number1 = int(input("Enter number 1"))

然后创建一个变量,让我们说result并为其分配数字的总和:

result = Number1 + Number2

最后打印结果

print "Result = " + str(result)

最终代码应如下所示:

print ("This is a calculator program, press Enter to continue")
a = input()

while a == "":
    print
    print("Enter 1 for option 1 which adds")
    print("Enter 2 for option 2 which subtracts")
    print("Enter 3 for option 3 which multiply")
    print("Enter 4 for option 4 which divides")
    print("Enter 5 for option 5 which quits",)

    Option = input("Enter an option number:")

    if Option == 1:
        Number1 = int(input("Enter number 1"))
        Number2 = int(input("Enter number 2"))
        result = Number1 + Number2
        print "Result = " + str(result) # To print you have to cast to `str`

    elif Option == 2:
        ...
    elif Option == 3:
        ...
    elif Option == 4:
        ...
    else:
        break

备注:

  • 您可以使用if-elif-else作为结构,因此如果Option == 1,则不会检查以下条件。

  • 我还建议您关注Python naming convention。您的变量Number1应该被称为number1,依此类推。

答案 4 :(得分:0)

我更正了你的代码。

_ = input("This is a calculator program, press Enter to continue")
print ("""Enter 1 for option 1 which adds
          Enter 2 for option 2 which subtracts
          Enter 3 for option 3 which multiplies
          Enter 4 for option 4 which divides
          Enter 5 for option 5 which quits""")
while True:
    Option = input("Enter an option number: ")

    if Option == '1':
        Number1 = int(input("Enter number 1: "))
        Number2 = int(input("Enter number 2: "))
        print("The Result is {0}".format(Number1 + Number2))

    elif Option == '2':
        Number1 = int(input("Enter number 1: "))
        Number2 = int(input("Enter number 2: "))
        print("The Result is {0}".format(Number1 - Number2))

    elif Option == '3':
        Number1 = int(input("Enter number 1: "))
        Number2 = int(input("Enter number 2: "))
        print("The Result is {0}".format(Number1 * Number2))

    elif Option == '4':
        Number1 = int(input("Enter number 1: "))
        Number2 = int(input("Enter number 2: "))
        print("The Result is {0}".format(Number1 / Number2))

    else:
        break

注意:

  1. 三重引用语法适用于长多行字符串。
  2. 格式化打印字符串的pythonic方法是str.format方法。
  3. 祝你学习好运!