Another Exercise (python)

Started by
11 comments, last by biggjoee5790 17 years, 1 month ago
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?
Advertisement
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?

Beginner in Game Development?  Read here. And read here.

 

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.
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.

Beginner in Game Development?  Read here. And read here.

 

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
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.
------------------------------Support the Blue Skies in Games Campaign!A blog... of sorts.As a general rule, if you don't have a general rule in your signature, you aren't as awesome as someone who does. General rules roxor teh big one one ones.
Ahh I think I got it, would this be correct?

Number=97Guess=0count=0while Guess != Number:    Guess = input("Guess A Number: ")    if Guess > Number:        print "Too High"        count = count + 1    elif Guess < Number:        print "Too Low"        count = count + 1print "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.

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.
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.

- Jason Astle-Adams

@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=97Guess=0count=0while Guess != Number:    Guess = input("Guess A Number: ")    if Guess > Number:        print "Too High"        count = count + 1    elif Guess < Number:        print "Too Low"        count = count + 1print "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?

Beginner in Game Development?  Read here. And read here.

 

This topic is closed to new replies.

Advertisement