Python Data structures

  • Thread starter WWGD
  • Start date
  • #1
WWGD
Science Advisor
Gold Member
5,675
5,782
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?
 

Answers and Replies

  • #2
35,431
7,296
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
pbuk
Science Advisor
Gold Member
2,638
1,341
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
      • ...
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.
 
  • #4
35,431
7,296
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
hmmm27
Gold Member
850
395
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:
  • #6
WWGD
Science Advisor
Gold Member
5,675
5,782
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
pbuk
Science Advisor
Gold Member
2,638
1,341
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
WWGD
Science Advisor
Gold Member
5,675
5,782
I need to goto read my books more carefully ;).
 
  • #9
pbuk
Science Advisor
Gold Member
2,638
1,341

Related Threads on Python Data structures

Replies
14
Views
3K
  • Last Post
Replies
2
Views
4K
Replies
1
Views
1K
  • Last Post
Replies
4
Views
3K
  • Last Post
Replies
1
Views
6K
  • Last Post
Replies
7
Views
1K
  • Last Post
Replies
2
Views
1K
  • Last Post
Replies
13
Views
6K
  • Last Post
Replies
3
Views
12K
  • Last Post
Replies
7
Views
3K
Top