从while循环中调用的函数返回一个值(Return a value from a function called in while loop)

编程入门 行业动态 更新时间:2024-10-28 01:23:35
从while循环中调用的函数返回一个值(Return a value from a function called in while loop)

重点是猜测从整数区间中选择的随机数,并在固定的尝试次数内进行。

main函数询问间隔的上限和用户可以给出的猜测数量。 然后核心函数应返回猜测值,因此当数字正确时,函数应立即终止。

我在调试时输入了一些print语句,我理解y值不会从core函数返回到while语句。

# -*- coding: utf-8 -*- def main(): from random import choice p = input("choose upper limit: ") t = input("how many attempts: ") pool = range(p+1) x = choice(pool) i = 1 while ((x != y) and (i < t)): core(x,y) i += 1 def core(x,y): y = input("choose a number: ") if y == x: print("You gussed the right number!") return y elif y > x: print("The number is lower, try again") return y else: print("The number is higher, try again") return y

The point is to guess a random number choosen from an interval of integers and do it within a fixed numbers of attempts.

The main function asks the upper limit of the interval and the number of guesses the user can give. The core function then should return the guessed value so when the number is right the function should terminate immediately.

I put some print statement while debugging and I understood that the y value is not returned to the while statement from the core function.

# -*- coding: utf-8 -*- def main(): from random import choice p = input("choose upper limit: ") t = input("how many attempts: ") pool = range(p+1) x = choice(pool) i = 1 while ((x != y) and (i < t)): core(x,y) i += 1 def core(x,y): y = input("choose a number: ") if y == x: print("You gussed the right number!") return y elif y > x: print("The number is lower, try again") return y else: print("The number is higher, try again") return y

最满意答案

您希望将core返回值分配回本地y变量,它不会通过引用传递:

y = core(x)

在进入循环之前,您还需要设置y 。 函数中的局部变量在其他函数中不可用。

因此,您根本不需要将y传递给core(x) :

def core(x): y = input("choose a number: ") if y == x: print("You gussed the right number!") return y elif y > x: print("The number is lower, try again") return y else: print("The number is higher, try again") return y

并且循环变为:

y = None while (x != y) and (i < t): y = core(x) i += 1

你在main()函数中设置y并不重要,只要它在用户做出猜测之前永远不会等于x 。

You want to assign the return value of core back to the local y variable, it's not passed by reference:

y = core(x)

You'll also need to set y before you go into the loop. Local variables in functions are not available in other functions.

As a result, you don't need to pass y to core(x) at all:

def core(x): y = input("choose a number: ") if y == x: print("You gussed the right number!") return y elif y > x: print("The number is lower, try again") return y else: print("The number is higher, try again") return y

and the loop becomes:

y = None while (x != y) and (i < t): y = core(x) i += 1

It doesn't much matter what you set y to in the main() function to start with, as long as it'll never be equal to x before the user has made a guess.

更多推荐

本文发布于:2023-07-28 12:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1305216.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   Return   function   called   loop

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!