用户输入了两个不同的雪佛兰和福特车队的8个比赛时间。 宣布每场比赛的获胜者。 以秒为单位显示他们获胜的时间。 宣布获胜最多的团队。
我创建了两个团队列表以及他们的时间。 我遇到的问题是如何每次都从列表中的位置撤出并声明该个人为获胜者。我的代码无法正常运行,我认为这与循环的设置有关。我也不想打印比赛时间的字符串,但是我不知道如何在没有代码的情况下获得谁的胜利的计算。 我只是在学习如何编码,所以请多多包涵。 我将在代码中添加注释,以表明我在说什么以帮助创建更好的图片。
#Team Chevy
Chevy = [0.0] * 8
for i in range(8):
Chevy[i] = float(input("Enter Chevy race time's: "))
i = i + 1
for i in range(0,8):
print("Chevy", str(i+1) + ": ", Chevy[i]) # I want to be able to remove the print function so I don't have the times showing up with the input and the print. But it messes up my winner determination when I remove it.
#Team Ford
Ford = [0.0] * 8
for j in range(8):
Ford[j] = float(input("Enter Ford race time's: "))
j = j + 1
for j in range(0,8):
print("Ford", str(j+1) + ": ", Ford[j])
#Determine winner
if Chevy[0] > Ford[0]:
winner = Chevy[0] - Ford[0]
print("Ford wins by",winner,"sec")
elif Ford[0] < Chevy[0]:
winner = Chevy[0] - Ford[0]
print("Chevy wins by",winner,"sec")
结果
Enter Chevy race time's: 1
Enter Chevy race time's: 2
Enter Chevy race time's: 3
Enter Chevy race time's: 4
Enter Chevy race time's: 5
Enter Chevy race time's: 6
Enter Chevy race time's: 7
Enter Chevy race time's: 8
Chevy 1: 1.0
Chevy 2: 2.0
Chevy 3: 3.0
Chevy 4: 4.0
Chevy 5: 5.0
Chevy 6: 6.0
Chevy 7: 7.0
Chevy 8: 8.0
Enter Ford race time's: 9
Enter Ford race time's: 8
Enter Ford race time's: 7
Enter Ford race time's: 6
Enter Ford race time's: 5
Enter Ford race time's: 4
Enter Ford race time's: 3
Enter Ford race time's: 2
Ford 1: 9.0
Ford 2: 8.0
Ford 3: 7.0
Ford 4: 6.0
Ford 5: 5.0
Ford 6: 4.0
Ford 7: 3.0
Ford 8: 2.0
Chevy wins by 8.0 sec
仅显示输入时间。 每场比赛的获胜者(比赛1是雪佛兰[0]和福特[0])。 宣布每场比赛或平局的获胜者。 宣布获胜最多的团队。
答案 0 :(得分:0)
Python提供了几种构建列表的方法,每种方法都有其优势,请尝试全部研究。该代码以三种方式执行。请注意,您并不总是需要索引“ i”来浏览列表。
R = int(input("Enter the number of races: "))
#Team Chevy
Chevy = []
for c in range(R):
Chevy.append(float(input("Enter Chevy race {} time's: ".format((c+1)))))
#
#Team Ford
Ford = [0.0] * R
for f, F in enumerate(Ford, start=1):
Ford[f-1] = float(input("Enter Ford race {} time's: ".format(f)))
#
#Determine winner
k = 1
for Chevy, Ford in zip(Chevy, Ford):
delta_time = Chevy - Ford
if delta_time < 0 :
print("Chevy wins race {} by {} s.".format(k, (-delta_time)))
if delta_time > 0 :
print("Ford wins race {} by {} s.".format(k, delta_time))
if delta_time == 0 :
print("The race {} was a Draw with the time of {} s.".format(k, Chevy))
k += 1
#
答案 1 :(得分:0)
这是我想出的:
print ('Welcome to the Chevy vs. Ford Drag Races!')
ford_time = []
chevy_time = []
ford_wins = 0
chevy_wins = 0
print("---Input Chevy Times---")
for i in range(1,9):
chevy_time.append(float(input("Enter time for Chevy Car "+str(i)+": ")))
print("\n---Input Ford Times---")
for i in range(1,9):
ford_time.append(float(input("Enter time for Ford Car "+str(i)+": ")))
print("\nAnd the winners are:\n")
for i in range(0,8):
if ford_time[i] > chevy_time[i]:
difference = ford_time[i] - chevy_time[i]
chevy_wins = chevy_wins+1
print("Chevy by {0:.2f} sec".format(difference))
elif ford_time[i] < chevy_time[i]:
difference = chevy_time[i] - ford_time[i]
ford_wins = ford_wins+1
print("Ford by {0:.2f} sec".format(difference))
else:
print("Tie!")
if ford_wins > chevy_wins:
print("\nAnd the winning team is: F O R D !")
elif ford_wins < chevy_wins:
print("\nAnd the winning team is: C H E V Y !")
else:
print("It's a T I E !")