簡體   English   中英

For 循環不起作用,只打印范圍內的第一個數字

[英]For-loop isn't working, only prints the first number in a range

我正在嘗試使用以下代碼打印數字 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")

輸出:
在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM