Hack Forums
[Python] Trying to make a &[b]#34[/b];guess the number&[b]#34[/b]; game, why doesn't this work? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Trying to make a &[b]#34[/b];guess the number&[b]#34[/b]; game, why doesn't this work? (/showthread.php?tid=3801630)



Trying to make a &[b]#34[/b];guess the number&[b]#34[/b]; game, why doesn't this work? - ๖ۣۜQ - Oct 12, 2013

Just started learning Python and I tried making a "guess the number" game where you set the minimum and maximum numbers yourself, but it gives me errors.

Could anyone tell me how to fix this?

Thanks in advance!
Code
import random

def chanceset():
    min_num = int(input("Enter the minimum number here: "))
    max_num = int(input("Enter the maximum number here: "))
    if max_num > min_num:
        print("Thank you! Minimum number set to " + str(min_num) + " and maximum number set to " + str(max_num) + ". Good luck!")
        cc = random.randint(min_num, max_num)
        game()
    else:
        print("Maximum number has to be higher than minimum number.")
        chance()
def game():
    print("")
    print("")
    pc = int(input("Enter a number between " + min_num + " and " + max_num + "."))
    if pc <= max_num and pc >= min_num:
        print("The computer's choice is " + cc + ".")
        print("Your choice is " + pc + ".")
        if cc == pc:
            print("Congratulations! You won!")
        else:
            print("That's too bad, you lost.")
    else:
        print("Your input must be beween " + min_num + " and " + max_num + ".")
chanceset()

These are the errors I'm getting:
Code
Traceback (most recent call last):
  File "Desktop/Python/guessthenumber.py", line 26, in <module>
    chanceset()
  File "Desktop/Python/guessthenumber.py", line 9, in chanceset
    game()
  File "Desktop/Python/guessthenumber.py", line 16, in game
    pc = int(input("Enter a number between " + min_num + " and " + max_num + "."))
NameError: global name 'min_num' is not defined



* - Skyla Blue - Oct 12, 2013

You need to put min_num and max_num in the global scope, i.e not inside a function.


* - monkeytherat - Oct 12, 2013

(Oct 12, 2013 12:45 PM)Skyla Blue Wrote:  You need to put min_num and max_num in the global scope, i.e not inside a function.

It's better form to pass them as parameters and define them in the game function.


* - glob - Oct 12, 2013

(Oct 12, 2013 12:59 PM)monkeytherat Wrote:  It's better form to pass them as parameters and define them in the game function.

There are hundreds of ways to improve the code, he just told OP what needed to be done to get the error to go away.


* - BeerMonger - Oct 14, 2013

(Oct 12, 2013 12:44 PM)๖ۣۜQ Wrote:  
Code
import random

def chanceset():
..
    else:
  print("Maximum number has to be higher than minimum number.")
  chance()
..
question, what does that "chance()" refer to?
Is it defined somewhere? am I missing something?


* - Skyla Blue - Oct 14, 2013

(Oct 14, 2013 01:22 AM)BeerMonger Wrote:  question, what does that "chance()" refer to?
Is it defined somewhere? am I missing something?

LOL good catch, I didn't see that. That would also cause a problem, after his other one.


* - BeerMonger - Oct 14, 2013

Thought I was being an idiot and missing something obvious XD
Woohoo I'm not an idiot 😝

Another thing is that in this line:
Code
pc = int(input("Enter a number between " + min_num + " and " + max_num + "."))
you need to convert the variables to string like so (or atleast I think you do):
Code
pc = int(input("Enter a number between " + str(min_num) + " and " + str(max_num) + "."))



* - Skyla Blue - Oct 14, 2013

(Oct 14, 2013 01:47 AM)BeerMonger Wrote:  ...

Why would you do that? Then you can't compare the two numbers, because they will be strings.


* - BeerMonger - Oct 14, 2013

Can you add ints to a string just like that? using "+"?
Cause when I tried it that didn't work.


* - Skyla Blue - Oct 14, 2013

(Oct 14, 2013 02:05 AM)BeerMonger Wrote:  Can you add ints to a string just like that? using "+"?
Cause when I tried it that didn't work.

No that doesn't work, you'd have to convert to one data type, or another.


* - BeerMonger - Oct 14, 2013

So I was right..You do need to convert them to strings then back to ints..
Or how would you do it in another way that wont convert them to strings Skyla?


* - Skyla Blue - Oct 14, 2013

(Oct 14, 2013 02:19 AM)BeerMonger Wrote:  So I was right..You do need to convert them to strings then back to ints..
Or how would you do it in another way that wont convert them to strings Skyla?

What part are you talking about? I'm confused.


* - BeerMonger - Oct 14, 2013

(Oct 14, 2013 01:47 AM)BeerMonger Wrote:  Another thing is that in this line:
Code
pc = int(input("Enter a number between " + min_num + " and " + max_num + "."))
you need to convert the variables to string like so (or atleast I think you do):
Code
pc = int(input("Enter a number between " + str(min_num) + " and " + str(max_num) + "."))

(Oct 14, 2013 02:01 AM)Skyla Blue Wrote:  Why would you do that? Then you can't compare the two numbers, because they will be strings.

You asked why would I do that and said I won't be able to compare the numbers..
But in order to do what the poster wanted to do you have to convert them to strings.. So I was right in that ^^

(only later on you will need to convert them back to int)


* - Skyla Blue - Oct 14, 2013

Oh you're right in that part, I thought you were talking about the actual comparing.


* - BeerMonger - Oct 14, 2013

KK, just a misunderstanding [:
Anyways Poster read the comments and see whats wrong in your code!