python - 与列表和长度一起使用的 while 语句

标签 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("price of the car:"))

因为它只会打印我输入的值

car=input("enter car name:")
price=float(input("price of the car:")) 

最佳答案

how to do it for a list?

使用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

关于python - 与列表和长度一起使用的 while 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48966738/

相关文章:

PHP MySQL While 循环 多个表

c++ - 将文件中的单词放入 HashMap (c++)

python - tf.Estimator 训练后检索张量 (Numpy) 值

python - Pip 说它成功安装了 mysqlclient,但找不到它或无法卸载它

java - for 循环的 onClickListener 不起作用

for-loop - for 循环 Arduino/C

vba range.find 方法在随机单元格上停止

python - 是否可以在 Python 中创建具有未知属性的类?

python - 在 PC 上同时运行 Python 2.7 和 3.5

Java io 流关闭错误