Jump to content
  • Advertisement
Sign in to follow this  
biggjoee5790

Another Exercise (python)

This topic is 4119 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!