Sign in to follow this  
biggjoee5790

Another Exercise (python)

Recommended Posts

hi everyone Im stuck at another problem in my Python learning. I have this program that has a user guess numbers until they get the correct one
Number=97
Guess=0
while Guess != Number:
    Guess = input("Guess A Number: ")
    if Guess > Number:
        print "Too High"
    elif Guess < Number:
        print "Too Low"
print "You Guessed It, The Number was 97!!"
the assignment is: Modify the higher or lower program from this section to keep track of how many times the user has entered the wrong number. If it is more than 3 times, print "That must have been complicated." i cant figure out how to go about doing this. The chapter is on if, elif, else, and while. so im guessing it can be done with just those ideas. can anyone help me out?

Share this post


Link to post
Share on other sites
I don't mean to sound harsh but you need to take a lookover on if/else statements. If you're wondering why, take a good look at the last 3 statements you have in that code. Also how are you keeping track of how many times someone has tried to guess the number?

Also what is the exact problem you are having? Are you getting an error? Are you getting the wrong or unexplained result?

Share this post


Link to post
Share on other sites
If you were refering to the fact that I had no indents, that was because I didnt paste it into the post correctly, should be ok now. I think my statements are fine other than that. And my problem is that I dont know how to have the program keep track of how many times the user inputs the wrong number, thats the help I need :) thanks

Oh yea I apoligize for having this thread made twice. I guess I accidently did that while editing or something. Im not sure how to delete the other post maybe the moderators can do it.

Share this post


Link to post
Share on other sites
Quote:
Original post by biggjoee5790
If you were refering to the fact that I had no indents, that was because I didnt paste it into the post correctly,

I realized that but it wasn't a problem so I didn't mention it.
Quote:
should be ok now.

Looks better for sure.
Quote:
I think my statements are fine other than that.

Oh no they're not. Look at that code again. That last statement is gonna print no matter what you enter. Have you tested the code thoroughly?
Quote:
And my problem is that I dont know how to have the program keep track of how many times the user inputs the wrong number, thats the help I need :) thanks

I'm pretty sure the tutorial or book you are using must have something in there about counters. If not just Google for while loops. You'll find something.
Quote:
Oh yea I apoligize for having this thread made twice. I guess I accidently did that while editing or something. Im not sure how to delete the other post maybe the moderators can do it.

Just go to the extra post and hit the checkbox on the top then hit the button at the bottom.

Share this post


Link to post
Share on other sites
I looked over it and ran it.. the last statement doesnt print unless the while statement gets satisfied. the while statement is: while guess != number. so that last statement wont print until the persons guess equals the number the program has. Im almost 100% sure its all correct, i ran the program and its fine

Share this post


Link to post
Share on other sites
All you need to do is have a variable that gets incremented whenever the user guesses too low (var += 1). When you exit the loop, check if it is over three. You can ask specific questions if you have trouble implementing this.

Share this post


Link to post
Share on other sites
Ahh I think I got it, would this be correct?

Number=97
Guess=0
count=0
while Guess != Number:
Guess = input("Guess A Number: ")
if Guess > Number:
print "Too High"
count = count + 1
elif Guess < Number:
print "Too Low"
count = count + 1
print "You Guessed It, The Number was 97!!"
if count > 3:
print "That must have been tough"



I ran the program and it seems to be working great. I tested it with all different amounts of wrong guesses and it only prints the last statement when you guess 3 or more wrong choices.

Share this post


Link to post
Share on other sites
This seemed like the best way to do it, Im not sure about other ways but based solely on what Ive learned so far, I really couldnt think of any other ways.

Share this post


Link to post
Share on other sites
You've correctly solved the problem, your program exhibits the desired behavior. I don't of course know exactly what you have and havn't learned already, but based purely on what you've shown us so far I'd say that's probably as good as any other solution you could produce with your current knowledge.

One thing you could do that's just a bit tidier would be to use count += 1 rather than count = count + 1. The += operator adds the value on the right to the variable on the left, so the two statements are equivalent.

Share this post


Link to post
Share on other sites
@OP: I apologize for the posts earlier. Obviously I was wrong to think that Python code translates directly to C. And/or I can't read code for jack.

So with that said, a question.

Number=97
Guess=0
count=0
while Guess != Number:
Guess = input("Guess A Number: ")
if Guess > Number:
print "Too High"
count = count + 1
elif Guess < Number:
print "Too Low"
count = count + 1
print "You Guessed It, The Number was 97!!"
if count > 3:
print "That must have been tough"

That code if I'm looking at straight looks like it should be this in C:

int number = 97, guess = 0, count = 0;
while (guess != number) {
cout << "Guess a number: ";
cin >> guess;
if (guess > number) {
cout << "Too High" << endl;
count = count + 1;
}
else if (guess < number) {
cout << "Too Low" << endl;
count = count + 1;
}
}
cout << You Guessed It, The Number was 97!!" << endl;
if (count > 3)
cout << "That must have been tough." << endl;


The translation is from what I currently know about Python indentation being like C brackets. So seeing that my knowledge is lacking, can someone be kind enough to tell how that Python program is actually working?

Share this post


Link to post
Share on other sites
Well, I'll see if I can explain the python code
(note to the OP: This tutorial is also availble at http://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python and contains solutions to all the excersises embedded in the wikibook.
Python runs in the order its typed in. Thusly the global variables number, guess and count are defined.
Next it sees the while guess != number statement.
Since guess (0) does not equal number (97), it follows the indented statement below it.
Next is the if statement. Depending on your guess (gotten from input), python determines whether to run the indenting below the if or the elif statement. Assuming you guessed wrong the first time, python is still in the while guess != number control statement and will continue to follow it, until such a time that you do guess right. When the correct number is entered, python stops looking at the while statement and goes to the next line, which prints out the verification of the user's success. After that, it checks to see if count > 3. If it is, the program prints out that additional intented line. If not, the program is finished, and depending on your setup, the console window may or may not wink out of existence.

Share this post


Link to post
Share on other sites
Ahhh, I see. The problem isn't the code or my translation. It's just my reading comprehension [grin] I thought the code was supposed to give the user three chances to guess and if the user took more than three guesses then it would print "It's too hard." only. Oops.

Share this post


Link to post
Share on other sites
Quote:
Original post by Alpha_ProgDes
Ahhh, I see. The problem isn't the code or my translation. It's just my reading comprehension [grin] I thought the code was supposed to give the user three chances to guess and if the user took more than three guesses then it would print "It's too hard." only. Oops.



Ahh I see what you thought, thats also what I thought originally. But no, the user can guess as many times as they want, but if its over 3, the message appears. Thanks to everyone who looked at my program and helped me figure out what to do.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this