0

这是我为学校写的一些代码。问题是,如果我继续添加更多“输入”函数,即使用的行和我使用的“和”函数的数量,它会在两个方面变得非常长。它是如此之大,以至于它使我的学校作业页面滞后。如果你想用 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")
4

2 回答 2

1

这可以在没有任何列表或特殊语法的情况下完成,只要您使用一个简单的循环来运行正文六次:

largest = None
for _ in range(6):
    number = int(input('Enter a number: '))
    if largest is None or number > largest:
        largest = number
    print('Largest: {}\n'.format(largest))

如果由于某种原因不允许使用循环,则可以通过手动展开循环来获得相同的结果:

largest = int(input('Enter a number: '))
print('Largest: {}\n'.format(largest))
number = int(input('Enter a number: '))
if number > largest:
    largest = number
print('Largest: {}\n'.format(largest))
number = int(input('Enter a number: '))
if number > largest:
    largest = number
print('Largest: {}\n'.format(largest))
number = int(input('Enter a number: '))
if number > largest:
    largest = number
print('Largest: {}\n'.format(largest))
number = int(input('Enter a number: '))
if number > largest:
    largest = number
print('Largest: {}\n'.format(largest))
number = int(input('Enter a number: '))
if number > largest:
    largest = number
print('Largest: {}\n'.format(largest))
于 2018-10-30T19:25:50.550 回答
0

您可以将数字存储在变量中,并在用户输入更大的变量时覆盖它。

awesome_number = 0
while True: # Always loop unless we break the loop.
    userInput = int(input("Enter a number:")) # Your input phrase

    # Compare values, evaluates to True if the user input is greater
    if userInput > awesome_number: 
        awesome_number = userInput # Overwrite if compare evaluates to True
    # Print using the .format method to dynamically insert the greatest
    # number
    print("So far, the biggest number you've entered is {}".format(awesome_number))

如果您想在 n 次输入后停止,您可以添加一个简单的计数器来计算迭代次数。

awesome_number = 0
counter = 0
while True: # Always loop unless we break the loop.
    userInput = int(input("Enter a number:")) # Your input phrase

    # Compare values, evaluates to True if the user input is greater
    if userInput > awesome_number: 
        awesome_number = userInput # Overwrite if compare evaluates to True
    # Print using the .format method to dynamically insert the greatest
    # number
    print("So far, the biggest number you've entered is {}".format(awesome_number))
    # Increment the counter
    counter = counter + 1
    if counter == 500: # Arbitrary number to end on
        break
于 2018-10-30T19:21:37.710 回答