Python Data Structures: Guessing a Number

In summary: 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.
  • #1
WWGD
Science Advisor
Gold Member
7,010
10,470
TL;DR Summary
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
  • #2
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
  • #3
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
  • #4
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
  • #5
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
  • #6
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.
 
  • #7
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:
 
  • #8
I need to goto read my books more carefully ;).
 
  • #9
  • Like
Likes WWGD

1. What is a data structure in Python?

A data structure in Python is a way of organizing and storing data in a computer so that it can be accessed and modified efficiently. It provides a means to manage large amounts of data and perform various operations on it.

2. What is the purpose of "Guessing a Number" game using Python data structures?

The purpose of "Guessing a Number" game using Python data structures is to demonstrate how data structures can be used to store and manipulate data in a fun and interactive way. It also helps in understanding the concept of data structures in Python.

3. What are some commonly used data structures in Python?

Some commonly used data structures in Python include lists, tuples, dictionaries, and sets. These data structures have different properties and are suitable for different types of applications.

4. How does Python data structures help in implementing the "Guessing a Number" game?

Python data structures help in implementing the "Guessing a Number" game by providing a way to store and retrieve the numbers guessed by the player. For example, a list can be used to store the numbers guessed by the player and a dictionary can be used to keep track of the number of attempts made by the player.

5. How can I improve the efficiency of the "Guessing a Number" game using Python data structures?

One way to improve the efficiency of the "Guessing a Number" game using Python data structures is to use a binary search algorithm to guess the number. This algorithm uses the properties of certain data structures, such as lists, to quickly narrow down the possible numbers and make the game more efficient.

Similar threads

  • Programming and Computer Science
Replies
1
Views
285
  • Programming and Computer Science
Replies
7
Views
491
  • Programming and Computer Science
Replies
7
Views
439
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
15
Views
1K
Replies
7
Views
244
  • Programming and Computer Science
Replies
1
Views
2K
Replies
9
Views
1K
  • Programming and Computer Science
Replies
14
Views
31K
Replies
10
Views
961
Back
Top