如何在Python中循环特定的代码段?

如何在Python中循环特定的代码段?,python,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, ple

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

#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')

您可以有一个函数,在每次调整后可以再次调用它自己

让我们调用此函数
adjust()

Python中的基本函数如下所示:

def function( parameters, if any):
   #does something
   #does another thing
   return 
#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')
answer=input()

while answer != 'correct':
    if answer == 'low':
        ....
    if answer == 'high':
        ....
#code for correct comes after the while loop
因此,让我们将调整“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')
在您最初猜测之后调用它,这样您的代码看起来像这样:

def function( parameters, if any):
   #does something
   #does another thing
   return 
#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')
answer=input()

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

你可能还想考虑将if语句链改成一系列if if IFS。

你要找的是一个循环。 首先,您需要确定一个条件,循环将使用该条件来知道何时完成

while answer != 'correct':
然后循环体在后面,所以整个过程是这样的:

def function( parameters, if any):
   #does something
   #does another thing
   return 
#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')
answer=input()

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

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

由@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')

您正在查找
while
循环。请看,Python循环非常直观。从目前为止您所知道的情况来看,您尝试了什么?旁注:假设这是针对一个类,他们可能希望您执行二进制搜索(可能是从一个随机的起点开始,只是为了让它有趣)。一次调整一个猜测意味着
O(n)
猜测,而二进制搜索只需要
O(logn)
猜测。对于这样的小范围,线性搜索并不可怕,但是如果猜测游戏进入更大的范围,一次递增或递减一个将花费永远的时间。我不确定OP是否准备好递归。此外,由于Python没有对尾部调用进行优化,如果列表足够大,这将导致堆栈溢出。我尝试了这个方法,我肯定知道这将如何解决我的问题,但它一直说调整没有定义?