如何在Python中循环执行代码的特定部分?

时间:2018-10-02 18:20:31

标签: python

我是一个极端的新手,才刚刚开始。我想学习如何循环代码的指定部分。

#This program ask you to choose a number between 1 and 20, then tries to 
#guess it.

import random

print('Think of a number between 1 and 20, im going to try and guess it.')
print('If my guess is too high or low, please tell me and ill guess again.')
input()

guess=random.randint(1, 20)
print('Is this too high, too low, or correct?')
print(guess)
answer=input()

#Until the user answers 'correct' I would
#like all of the following to loop:
if answer == 'low':
    print('how about now?')
    guess=guess+1
    print(guess)
if answer == 'high':
    print('how about now?')
    guess=guess-1
    print(guess)
if answer == 'correct':
    print('nice!')
    print()
    print('Press CTRL+C to exit')

3 个答案:

答案 0 :(得分:0)

您正在寻找的是while循环。

首先,您需要确定循环将用来知道何时完成的条件。

while answer != 'correct':

然后是循环的主体,所以整个过程都是这样的:

answer=input()

while answer != 'correct':
    if answer == 'low':
        ....
    if answer == 'high':
        ....
#code for correct comes after the while loop

需要考虑的事情:如果答复既不是“正确”,“低”也不是“高”怎么办?

答案 1 :(得分:0)

@munk建议的while循环结束了。看起来像这样。

#This program ask you to choose a number between 1 and 20, then tries to guess it.
import random
print('Think of a number between 1 and 20, im going to try and guess it.')
print('If my guess is too high or low, please tell me and ill guess again.')
input()

guess=random.randint(1, 20)
answer=input()
print('Is this too high, too low, or correct?')
print(guess)
answer=input()

#answer=input() at end of if statements is necessary to keep them from repeating
while answer != 'correct' :
    if answer == 'low':
        print('how about now?')
        guess=guess+1
        print(guess)
        answer=input()
    if answer == 'high':
        print('how about now?')
        guess=guess-1
        print(guess)
        answer=input()


 if answer == 'correct':
    print('nice!')
    print()
    print('Press CTRL+C to exit')

答案 2 :(得分:-1)

您可以使用一个函数,该函数可以在每次调整后再次调用自身。

我们将此函数称为adjust()

Python中的基本功能如下:

def function( parameters, if any):
   #does something
   #does another thing
   return 

因此,让我们将调整“ if语句”放入adjust()函数中。如果用户回答“低”或“高”,adjust将再次调用自己。

def adjust():

    print('Is this too high, too low, or correct?')

    print(guess)

    answer=input()

    #Until the user answers 'correct' I would
    #like all of the following to loop:

    if answer == 'low':
        print('how about now?')
        guess=guess+1
        print(guess)
        adjust()

    if answer == 'high':
        print('how about now?')
        guess=guess-1
        print(guess)
        adjust()

    if answer == 'correct':
        print('nice!')
        print()
        print('Press CTRL+C to exit')

在您最初的猜测之后调用它,这样您的代码看起来像这样:

#This program ask you to choose a number between 1 and 20, then tries to 
#guess it.
import sys
import random

print('Think of a number between 1 and 20, im going to try and guess it.')
print('If my guess is too high or low, please tell me and ill guess again.')
input()
guess=random.randint(1, 20)
adjust()

def adjust():

        print('Is this too high, too low, or correct?')

        print(guess)

        answer=input()

        #Until the user answers 'correct' I would
        #like all of the following to loop:

        if answer == 'low':
            print('how about now?')
            guess=guess+1
            print(guess)
            adjust()

        if answer == 'high':
            print('how about now?')
            guess=guess-1
            print(guess)
            adjust()

        if answer == 'correct':
            print('nice!')
            print()
            sys.exit(0) #quit the program
            #print('Press CTRL+C to exit')

您可能还需要考虑将if语句链改为一系列If-Else Ifs。