python - For-loop 不起作用,只打印范围内的第一个数字
问题描述
我正在尝试使用以下代码打印数字 20 到 25:
for index in range(20, 26):
print(index)
此代码位于其他代码的中间,不会以任何方式与之交互。但是由于某种原因,它只打印了这个范围的第一个数字,20。这可能是因为循环结束很短,因为它后面有代码,我不确定。我希望这是足够的描述性,但我只想显示这段代码:
20
21
22
23
24
25
垂直。
编辑:
这是完整的代码:
def allow(will_wont):
print(will_wont + ", ok sure")
if will_wont == "yes":
import time
numbers = ["zero", "one", "two", "three", "four", "four", "seventeen"]
integers = [1, 9, 7, 6, 8, 2, 3, 10, 3, 4, 3, 1, 7]
loop_number = 8
numbers.insert(5, "five")
time.sleep(0.5)
integers.sort()
print(numbers.index("one"))
time.sleep(0.5)
print(numbers.count("four"))
time.sleep(0.5)
print(numbers[3])
time.sleep(0.5)
number = input("what number comes next: ")
if number == "four" or 4:
print("correct, it was four")
else:
print("fail, it was four")
time.sleep(0.5)
time.sleep(0.5)
print(numbers[5])
time.sleep(0.5)
print(integers[7])
time.sleep(0.5)
def num(num1, num2):
if num1 > num2:
return num1
elif num2 > num1:
return num2
print(num(7, 5))
def num(num1, num2):
if num1 > num2:
return num1
elif num2 > num1:
return num2
time.sleep(0.5)
small_number = input("select a number smaller than 8 (in int form pls):")
time.sleep(0.5)
print("your number was" + small_number)
time.sleep(0.5)
if int(small_number) < 8:
print("8> " + small_number + " thus your choice doesn't matter and 8 will be chosen anyway, take an L")
else:
print("you didn't listen to me, so the number will now be ")
time.sleep(1)
print("8")
time.sleep(1)
print("time for loops")
while loop_number <= 15:
loop_number += 1
time.sleep(0.5)
print(loop_number)
time.sleep(0.5)
print("end of loop")
time.sleep(0.5)
print(numbers[7])
time.sleep(0.5)
print("guess what number comes next (use lowercase text bcos im bad at code) ")
guess_number = "eighteen"
guess = ""
while guess != guess_number:
guess = input("guess here: ")
time.sleep(0.5)
print("nicely done, good job counting! ")
time.sleep(0.5)
print("this time you only get 3 guesses, try guessing the next number, lowercase text pls")
guess_number2 = "nighteen"
guess2 = ""
guess_count = 0
guess_limit = 3
no_more_guesses = False
finished = False
while guess2 != guess_number2 and not no_more_guesses and not finished:
if guess_count < guess_limit:
guess2 = input("enter guess here: ")
guess_count += 1
else:
no_more_guesses = True
if no_more_guesses:
print("fail, the correct guess was nineteen")
elif guess2 == "nineteen":
print("correct the number was nineteen")
finished = True
time.sleep(0.5)
print("index time")
print("for index in range 20-25")
for index in range(20, 26):
print(index)
input("damn my index doesn't work properly it only printed the fist number, idk why")
outro = input("do you want to hear my outro? yes or no?")
if outro == "yes":
解决方案
# code snippet from the end of the code you provided
for index in range(20, 26):
print(index)
input("damn my index doesn't work properly it only printed the fist number, idk why")
outro = input("do you want to hear my outro? yes or no?")
if outro == "yes":
print("Too bad")
我假设你想这样做:
for index in range(20, 26):
print(index)
# input("damn my index doesn't work properly it only printed the fist number, idk why")
print("My index works now")
time.sleep(0.5)
# Indent the remaining part out of the for loop
outro = input("do you want to hear my outro? yes or no?")
if outro == "yes":
print("Too bad")
推荐阅读
- c++ - 使用 openCV 编译程序时出现 Collect2 ld 错误
- css - 高度为 100% 的 CSS 网格子元素溢出父元素
- node.js - Ng Serve 返回“对象原型可能只是一个对象或空:未定义类型错误:对象原型可能只是一个对象或空:未定义”
- perl - (Perl)从文件中读取字符串时是否可以插入变量?
- sql - 如何通过 Spring Boot JPA 执行具有 INTERVAL 子句的本机 SQL 查询?
- html - Radio Checked 元素选择 Sass
- javascript - 如何在列表视图上按下按钮时编写发送电子邮件的代码
- javascript - 如何将datatable javaScript数据导出到eccel,不导出隐藏列,但不影响隐藏响应数据?
- python - 使用 Matplotlib.Pyplot 显示非规范化图像会导致多色像素马赛克:为什么?
- apache-camel - 使用骆驼 pollEnrich 合并 xml 输出