Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.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 while语句,该语句与列表和长度一起使用_Python_For Loop_While Loop - Fatal编程技术网

Python while语句,该语句与列表和长度一起使用

Python while语句,该语句与列表和长度一起使用,python,for-loop,while-loop,Python,For Loop,While Loop,如果我有这个清单 car=['Mazda','ford','Honda','corvette','Mercedes'] price=['300','450','350','490','500'] 我想把下面的句子一次打印出来 print ("that", car,"car price is:",price ,"in thousands dollar") 如何写我通常使用的条件,但是如何写列表 car=input("enter car name:") price=float(input("pr

如果我有这个清单

car=['Mazda','ford','Honda','corvette','Mercedes']
price=['300','450','350','490','500']
我想把下面的句子一次打印出来

print ("that", car,"car price is:",price ,"in thousands dollar")
如何写我通常使用的条件,但是如何写列表

car=input("enter car name:")
price=float(input("price of the car:"))
因为它只会打印我输入的值

car=input("enter car name:")
price=float(input("price of the car:")) 
如何制作列表

使用
zip()
函数:

cars = ['Mazda','ford','Honda','corvette','Mercedes']
prices = ['300','450','350','490','500']

for car, price in zip(cars, prices):
    print("that {} car price is: {} in thousands dollar".format(car, price))
输出:

that Mazda car price is: 300 in thousands dollar
that ford car price is: 450 in thousands dollar
that Honda car price is: 350 in thousands dollar
that corvette car price is: 490 in thousands dollar
that Mercedes car price is: 500 in thousands dollar

首先使用for循环:

car = ['Mazda', 'ford', 'Honda', 'corvette', 'Mercedes']
price = ['300', '450', '350', '490', '500']
for i in range(len(car)):
    print ("that", car[i],"car price is:",price[i] ,"in thousands dollar")
然后是while循环:

i = 0
while(i < len(car)):
    print ("that", car[i],"car price is:",price[i] ,"in thousands dollar")
    i += 1
i=0
而(我(车)):
打印(“该”,汽车[i],“汽车价格为:”,价格[i],“以千美元为单位”)
i+=1
演示链接


您可以压缩这两个列表,这将起作用,但我认为字典是完成此任务的合适数据结构

car_prices = {
'Mazda': 300,
'Ford': 450,
'Honda': 350,
'Corvette': 490,
'Mercedes': 500
}

for car, price in car_prices.items():
 print("that {} car price is: {} in thousands dollar".format(car,price))

希望它能有所帮助

列表是已知的还是依赖用户输入?我尝试了您的代码,但它给了我这个错误i+=1^缩进错误:未缩进不匹配任何外部缩进级别我重试,它给了我一个句子中所有列表的组合,这也是一个无限循环这是一个无限输出在我看来,['Mazda'、'ford'、'Honda'、'corvette'、'Mercedes']汽车价格是:['300'、'450'、'350'、'490'、'500']以千美元为单位,你可以检查我在回答中提到的上面的链接,运行代码onceyeah作为水流,谢谢,然而,我尝试运行的代码有点复杂,当我尝试执行与你相同的代码时,它总是在i+=1处给我缩进错误,如果我想使用这个len(list([list_name]),这是否合适?@kimkasama我不确定我是否知道你的意思,但你可以通过简单地使用len(car_prices)获得字典的长度