Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Guess a number
#4
Recursive invocation is limited by depth.

count = 0
def main():
    global count
    count += 1
    return count

main()
This code leads to

Output:
File "<stdin>", line 4, in main File "<stdin>", line 4, in main RuntimeError: maximum recursion depth exceeded >>> count 999
So, the program can't produce too much queries (but this behavior (recursion depth) could be tweaked).

Using infinite loop is more preferable in this case.

from random import randint
it = randint(0, 101)
def guess():
    while True:
        x = int(input('Guess a number one through one hundred: '))
        if x == it:
            print("You got it!")
            break
        elif x > it:
            print("too high")
        else:
            print("too low")
guess()
Reply


Messages In This Thread
Guess a number - by Ameen - Apr-02-2018, 01:03 AM
RE: Guess a number - by ljmetzger - Apr-02-2018, 09:22 PM
RE: Guess a number - by Ares - Apr-03-2018, 01:50 AM
RE: Guess a number - by scidam - Apr-03-2018, 03:35 AM
RE: Guess a number - by sylas - Apr-03-2018, 07:28 AM
RE: Guess a number - by snippsat - Apr-03-2018, 01:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Guess the word game help jackthechampion 3 3,140 Sep-06-2023, 06:51 AM
Last Post: Pedroski55
  Ask again if wrong guess with While. EduPy 4 2,367 Oct-21-2021, 07:46 PM
Last Post: menator01
  I guess it's about print tsavoSG 2 2,208 Feb-08-2021, 08:34 PM
Last Post: steve_shambles
  can't figure out problem with number guess Galoxys 4 3,445 Oct-29-2018, 01:45 PM
Last Post: snippsat
  Guess Random Number Why i m not able to enter input Nithya Thiyagarajan 6 8,358 Jan-07-2018, 04:26 AM
Last Post: squenson
  "Guess the Number" game CTT 14 13,638 Jul-26-2017, 11:41 AM
Last Post: sylas

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020