为什么不获取输入值? “蟒蛇”

时间:2019-11-11 19:55:36

标签: if-statement input printing

正如您所看到的,

并没有为if提供输入值!为什么?以及如何将输入值存储到变量(数字)

const data = [
    {
        year: 2001,
        car: 'Audi',
        nrOfDoors: 5,
    },
    {
        year: 2010,
        car: 'Audi',
        nrOfDoors: 3,
        milage: 2000,
    },
    {
        year: 2019,
        car: 'Audi',
        nrOfDoors: 5,
        milage: 2000,
    },
    {
        year: 2005,
        car: 'Audi',
        nrOfDoors: 5,
        milage: 2000,
    },
    {
        year: 2009,
        car: 'Audi',
        nrOfDoors: 5,
        milage: 2000,
    },
    {
        year: 2002,
        car: 'Audi',
        nrOfDoors: 4,
        milage: 2000,
    },
    {
        year: 2001,
        car: 'Audi',
        nrOfDoors: 3,
        milage: 2000,
    },
    {
        year: 2001,
        car: 'Audi',
        nrOfDoors: 5,
        milage: 2000,
    },
];

const newList = data.reduce((acc, d) => {
    if(!acc[d.year]) {
        acc[d.year] = 0;
    }

    acc[d.year] += d.milage ? d.milage : 0;

    return acc;
}, {});
number = input(f"Enter a Number? ")
if number == (1):
    print("one")
if number == (2):
    print("two or greater")
else:
    print("negative number")

1 个答案:

答案 0 :(得分:0)

存在一些问题,通常当您使用input并希望提交数字时,您想在使用int()之前先确保将输入理解为数字(int类型) 。此外,当您要声明“ else-if”语句时,应使用elif。最后,在您的if语句中不需要括号。以下应该起作用:

number = int(input("Enter a Number? "))
if number == 1:
    print("one")
elif number >= 2: #I've changed this syntax, because two or greater is written like this
    print("two or greater")
else:
    print("negative number")

这是我正在尝试的输出:

Enter a Number? 3
two or greater