这是我为学校写的一些代码。问题是,如果我继续添加更多“输入”函数,即使用的行和我使用的“和”函数的数量,它会在两个方面变得非常长。它是如此之大,以至于它使我的学校作业页面滞后。如果你想用 7 个或更多的数字来做,那会让我的页面更加滞后。如何简化下面的代码?
作业:编写程序输入 6 个数字。输入每个数字后,打印迄今为止输入的最大数字。
num1 = int(input("Enter a number:"))
print ("Largest: " + str(num1) + "\n")
num2 = int(input("Enter a number:"))
if (num1 > num2):
print ("Largest: " + str(num1) + "\n")
else:
print ("Largest: " + str(num2) + "\n")
num3 = int(input("Enter a number:"))
if (num1 > num2 and num1 > num3):
print ("Largest: " + str(num1) + "\n")
elif (num2 > num3):
print ("Largest: " + str(num2) + "\n")
else:
print ("Largest: " + str(num3) + "\n")
num4 = int(input("Enter a number:"))
if (num1 > num2 and num1 > num3 and num1 > num4):
print ("Largest: " + str(num1) + "\n")
elif (num2 > num3 and num2 > num4):
print ("Largest: " + str(num2) + "\n")
elif (num3 > num4):
print ("Largest: " + str(num3) + "\n")
else:
print ("Largest: " + str(num4) + "\n")
num5 = int(input("Enter a number:"))
if (num1 > num2 and num1 > num3 and num1 > num4 and num1 > num5):
print ("Largest: " + str(num1) + "\n")
elif (num2 > num3 and num2 > num4 and num2 > num5):
print ("Largest: " + str(num2) + "\n")
elif (num3 > num4 and num3 > num5):
print ("Largest: " + str(num3) + "\n")
elif (num4 > num5):
print ("Largest: " + str(num4) + "\n")
else:
print ("Largest: " + str(num5) + "\n")
num6 = int(input("Enter a number:"))
if (num1 > num2 and num1 > num3 and num1 > num4 and num1 > num5 and num1 > num6):
print ("Largest: " + str(num1) + "\n")
elif (num2 > num3 and num2 > num4 and num2 > num5 and num2 > num6):
print ("Largest: " + str(num2) + "\n")
elif (num3 > num4 and num3 > num5 and num3 > num6):
print ("Largest: " + str(num3) + "\n")
elif (num4 > num5 and num4 > num6):
print ("Largest: " + str(num4) + "\n")
elif (num5 > num6):
print ("Largest: " + str(num5) + "\n")
else:
print ("Largest: " + str(num6) + "\n")