小编Gar*_*yne的帖子

在python的一系列输入中查找缺失的数字

我在完成这项任务时遇到了麻烦。我不能使用高级方法。我的教授说这只能使用循环来完成。没有函数,没有数学课,没有列表,什么都没有。

用户输入一个大于或等于 1 的数字 (n)。然后,代码要求用户输入与 n-1 一样多的数字,然后代码应该打印出用户没有输入的缺失数字'进入。所以:

示例运行:

请输入 n:5

请输入数字:4

请输入数字:5

请输入一个数字:1

请输入一个数字:2

缺少的数字是:3

-------------- 这是我到目前为止所拥有的,但它不起作用。它仅在按顺序输入数字时才有效。

n = int(input("Please enter n: "))

counter = 1
holder = -1

 while (counter < n):
     x = int(input("Please enter a number: "))
     if x != counter:
          holder = counter
     counter += 1

print("This missing number is " + str(holder))
Run Code Online (Sandbox Code Playgroud)

python loops numbers input while-loop

0
推荐指数
1
解决办法
573
查看次数

标签 统计

input ×1

loops ×1

numbers ×1

python ×1

while-loop ×1