0

So I am a newbie to python, i am working on trying to see how I can rewrite a code that is trying to wait on 2 different variables I believe, this is what I have so far.

def generate_random():
    number = random.randint(1,10)
    return number

def main() :
        guessed_number = int(input("Please select a number between 1 and 10 "))
        if guessed_number > 1 and guessed_number <= 10:
            random_number = generate_random()
            if guessed_number == random_number:
                print("Successful guess!")
            else:
                 print("Unsuccessful guess")
        else:
             print("You need to choose a number between 1 and 10")

if __name__ == "__main__":
     main()

I haven't really tried much in trying to fix it, just curious which directions i can go

DaveyBoy
  • 2,928
  • 2
  • 17
  • 27
McDubs
  • 1
  • Please update your question with the formatted *text* of your code. – quamrana Jul 15 '23 at 17:15
  • Don't post links to images because we can't copy/paste/adujst it. Actually paste the code and enclose it in the code markers (use the "formatting help" to do this. I've edited the question for you, but remember this in future – DaveyBoy Jul 15 '23 at 17:15
  • Should the while loop act as a way to verify you correctly guessed? E.g. loop until you get the right number, then quit? – B Remmelzwaal Jul 15 '23 at 17:24
  • #B Remmelzwaal, technically that is one half of what i was looking for, my teacher basically said that the if/and statement of the >1 and <10 could be changed into a while loop, he just couldnt remember how. Hope that helps – McDubs Jul 15 '23 at 17:33

2 Answers2

0

Sorry, I really didn't know what you are looking for. So I hope you don't mind if I fixed your code and added my own flair in the process:

import random

def generate_random():
    number = random.randint(1,10)
    return number

def main() :
    correctValue = False
    random_number = generate_random()


    while correctValue != True:
        guessed_number = int(input("Please select a number (1 - 10): "))
        if guessed_number >= 1 and guessed_number <= 10:
            if guessed_number == random_number:
                print("Successful guess!")
                correctValue = True
            elif guessed_number < random_number:
                print("Smaller then guessed number")
            elif guessed_number > random_number:
                print("Larger then guessed number")
            else:
                 print("Unsuccessful guess")
        else:
             print("You need to choose a number between 1 and 10")

if __name__ == "__main__":
     main()
Erik H
  • 1
  • 2
  • Would recommend you use my code above, dissect it and compare it to what you had. Dissecting working code is a great way to learn. – Erik H Jul 15 '23 at 17:41
  • This one is nice, when ran, it has a issue in there where even if you select the number 1 it says "Larger then guessed number – McDubs Jul 15 '23 at 17:43
  • I found out what was going on, You had the > and < signs reversed in the elif statement, i fixed it and its working wonderfully – McDubs Jul 15 '23 at 17:45
  • Glad it helps. Finding and solving bugs is another great way to learn :P – Erik H Jul 15 '23 at 17:59
  • For sure! i showed it to my class as an example, they all loved it! – McDubs Jul 15 '23 at 18:03
  • Upvote would be appreciated then :) – Erik H Jul 15 '23 at 18:17
-2

Did you mean that you want to loop over the whole program?

if __name__ == "__main__":
    while True:
        main()
quamrana
  • 37,849
  • 12
  • 53
  • 71
  • Maybe, i believe what I was trying to accomplish is changing the whole thing where if guessed_number > 1 and guessed_number <= 10: could be converted into a while loop instead of a if/and – McDubs Jul 15 '23 at 17:22
  • So the code I posted does that. Did you want something different? – quamrana Jul 15 '23 at 17:23
  • nope. sorry like said, im going through school for python. – McDubs Jul 15 '23 at 17:23
  • I'm asking how the code I posted does not perform the behaviour you are after. – quamrana Jul 15 '23 at 17:25
  • Terribly sorry, im not too sure how to elaborate on what i am looking for, my teacher stated there is a way to turn the if/and statement of the code into a while loop instead. I appreciate the responses you did give, it showed me something new. – McDubs Jul 15 '23 at 17:31
  • Do the answers to this [question](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) help at all? – quamrana Jul 15 '23 at 17:36
  • Yes helps alot into what i am trying to do, now to just implement that method of the while True: into my code. Thanks! – McDubs Jul 15 '23 at 17:38