Python Data Structures: Guessing a Number

Click For Summary

Discussion Overview

The discussion revolves around designing a Python program that prompts a user to guess a number within a finite range, providing feedback on whether the guess is too high or too low. Participants explore the necessary control structures and data structures for implementing this functionality, as well as the concept of using "goto" statements in programming.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant suggests using a control structure, specifically an if...else block within a while loop, to manage user input and feedback on guesses.
  • Another participant emphasizes that the original poster (OP) does not need a data structure for this task, but rather a control structure.
  • Some participants express skepticism about the utility of "goto" statements in modern programming, while others argue that there are legitimate use cases for them.
  • A participant mentions that many programming languages, including C and Ada, implement "goto" statements, countering the claim that only Fortran uses them effectively.
  • There is a humorous reference to a fictional "comefrom" statement in programming, indicating a light-hearted take on the discussion of control flow.
  • One participant acknowledges a misunderstanding regarding the terminology, clarifying that they meant "control structure" instead of "data structure."

Areas of Agreement / Disagreement

Participants generally agree that the OP needs a control structure rather than a data structure. However, there is disagreement regarding the relevance and utility of "goto" statements in programming, with multiple competing views presented.

Contextual Notes

Some participants reference the historical context and debates surrounding the use of "goto" statements, indicating that opinions on this topic may be influenced by personal experiences and programming paradigms.

WWGD
Science Advisor
Homework Helper
Messages
7,795
Reaction score
13,095
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   Reactions: 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   Reactions: 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   Reactions: 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   Reactions: 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 someone else would - perhaps I should have added a :wink:
 
I need to goto read my books more carefully ;).
 
  • Like
Likes   Reactions: WWGD

Similar threads

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