Python Python Data Structures: Guessing a Number

Click For Summary
The discussion centers on creating a Python program that prompts users to guess a number within a finite range, providing feedback on whether their guesses are too high or too low until they arrive at the correct answer. The original poster describes their current implementation using while loops and a counter but seeks advice on how to structure the feedback loop effectively. Participants emphasize that a control structure, specifically an if-else block within a while loop, is essential for handling the logic of the guessing game. The conversation briefly touches on the concept of "goto" statements, with participants debating their relevance and utility in programming languages. Ultimately, the consensus is that Python does not support "goto," and the focus should remain on using appropriate control structures to achieve the desired functionality in the guessing game.
WWGD
Science Advisor
Homework Helper
Messages
7,771
Reaction score
12,990
TL;DR
Want to loop using various conditions.
Hi all,
Trying to right a program in Python asking user to guess a number ( integer in finite range) until they make a correct guess, though I want to warn user when guess is too high --asking that they choose a lower number ,and same for when the guess is low, asking them to choose a higher number ,and ultimately pointing out when the guess is correct. I would like suggestions for the specific data structures to do this.

I have written a program that runs, prompting the user to guess until they guess correctly or exiting after a certain number of tries, using while loops and a counter variable that increases by 1 upon each guess.

Problem comes in designing a loop that goes back-and-forth between " overshoots (guess is too high)" and "undershoots( guess is too low)" until they guess the correct number ,whereby program exits:
Say the number is 10: If user enters , day, 9, they will be prompted to guess higher. But if they then choose , say, 11 and overshoot, how can I then prompt them to go back to guessing lower? Is there something along the lines of a" Go To" in Python? Maybe something else?Any ideas on how to go about doing this?
 
Technology news on Phys.org
You don't need a data structure -- you need a control structure. Namely, an if .. else block inside a while loop. The if ... else structure determines whether the guess was too low, too high, or right on the money, and displays an appropriate prompt to the user.
 
  • Like
Likes WWGD, hmmm27, pbuk and 1 other person
WWGD said:
Any ideas on how to go about doing this?
With practice writing code to do this becomes second nature, but this time round how about writing down the procedure in natural language:
  • Let the answer be a pseudo-random integer in the range [1, 99]
  • Let the number of guesses be 0
  • Ask the user to input a guess
  • While the guess is not the same as the answer and there have been less than 10 guesses:
    • add 1 to the number of guesses
    • If the guess is too low:
      • ...
    • otherwise
      • ...
WWGD said:
Is there something along the lines of a" Go To" in Python?
No. Forget about GOTO, the only useful language that implements GOTO is Fortran, and if FORTRAN had been developed later than the 1950s it wouldn't exist there either.
 
  • Like
Likes WWGD
pbuk said:
No. Forget about GOTO, the only useful language that implements GOTO is Fortran, and if FORTRAN had been developed later than the 1950s it wouldn't exist there either.
Agreed that the OP doesn't need goto, but I disagree with the rest of your statements.
Many languages provide a goto, among them C, C++, Pascal (but not Modula-2), and Ada, which BTW was developed in the 80s. Java does not have a goto statement, per se, but it does have a variant of a break statement that is a goto in all but name -- break <label>. I don't suppose that you are saying that the languages I listed are not useful.

In a different thread, I wrote this yesterday:
Granted, goto is usually frowned on, but there are legitimate reasons to use it, such as to bail out under extraordinary conditions. Some arguments pro, per Steve McConnell's "Code Complete," pp. 348,349.
A well-placed goto can eliminate the need for duplicate code.
The goto is useful in a routine that allocates resources, performs operations on those resource, and then deallocates the resources. With a goto, you can clean up in one section of code.
In some cases, the goto can result in faster and smaller code. Knuth's 1974 article cited a few cases in which the goto produces a legitimate gain.
Two decades' worth of research with gotos has failed to demonstrate their harmfulness. In a survey of the literature, B.A. Sheil concluded that unrealistic test conditions, poor data analysis, and inconclusive result failed to suppor the claim of Shneiderman and others that the number of bugs in code was proportional to the number of gotos (1981).
Finally, the goto was incorporated into the Ada language, the most carefully engineered programming language in history. Ada was developed long after the arguments on both sides of the goto debate had been fully developed.
 
  • Like
Likes pbuk and WWGD
WWGD said:
Is there something along the lines of a" Go To" in Python? Maybe something else?
Yes, it's called goto. There's also a comefrom statement (Who says compiler writers don't have a sense of humour ?).
 
Last edited:
  • Like
Likes WWGD
Mark44 said:
You don't need a data structure -- you need a control structure. Namely, an if .. else block inside a while loop. The if ... else structure determines whether the guess was too low, too high, or right on the money, and displays an appropriate prompt to the user.
Thanks, my bad, I meant control structure.
 
Mark44 said:
Agreed that the OP doesn't need goto, but I disagree with the rest of your statements.
I expected that you or somone else would - perhaps I should have added a :wink:
 
I need to goto read my books more carefully ;).
 
  • Like
Likes WWGD

Similar threads

  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 10 ·
Replies
10
Views
2K
Replies
86
Views
2K
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K