在python 3中定义一个函数

时间:2016-09-22 04:53:21

标签: python-3.x

当您猜到正确的数字时,它会冻结并且不会继续进行,但是如果我没有定义函数 do_guess_round 而只是在第二个之后编写代码而True 语句完美无缺。我想我错误地定义了函数

import random
computers_number = random.randint(1,100)
prompt=('what is your guess? ')

def do_guess_round():
    """choose a random number, prompt the user to guess it
    check whether the user is true, 
    and repeat the pocess untill the user is correct"""
while True:
    players_guess=input (prompt)
    if computers_number == int(players_guess):
      print ('correct! well done')
      break
    elif computers_number<int(players_guess):
      print ("incorrect, your guess is higher")
    else:
      print ('incorrect, your guess is lower')

print ("Starting a new round ")
print ("let the guessing game begin")      

while True:
    do_guess_round()  

3 个答案:

答案 0 :(得分:0)

当前您的do_guess_round()功能无效。您需要缩进应该包含在其中的代码。它下面的while True没有缩进,因此函数的主体当前只包含字符串:

"""choose a random number, prompt the user to guess it
check whether the user is true, 
and repeat the pocess untill the user is correct"""

答案 1 :(得分:0)

目前您猜对了正确的数字,您的代码就会崩溃到

BtnDelete_Click

毫不奇怪它冻结了,你还没有任何方法让它退出 - if (MessageBow.Show("Are you sure you want to Delete this File?", "Open File", MessageBoxButtons.YesNo) == DialogResult.Yes) { con.Open(); SqlConnection con = new SqlConnection(@"..........."); SqlCommand cmd = new SqlCommand("DELETE [DocumentName] from DocStorage where DocumentName=@DocShred", con); cmd.Connection = con; cmd.CommandType = CommandType.Text; cmd.Parameters.Add(new SqlParameter("@DocShred", listBox1.SelectedItem.ToString()); con.Close(); MessageBox.Show("Delete Successful", "File Deleted", MessageBoxButtons.Ok); } 只留下一个级别的循环。大概你要移动代码来生成函数中的数字?你需要更像

的东西
select MAX(STAGEID)
from [Your-table]
where average=100;

答案 2 :(得分:0)

def do_guess_round():
    """choose a random number, prompt the user to guess it
    check whether the user is true, 
    and repeat the pocess untill the user is correct"""
while True:

&#34;而真实&#34;循环不是缩进的,所以它不在函数中,在循环中缩进它的内容,那么你应该没事。