Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

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

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
You need to put min_num and max_num in the global scope, i.e not inside a function.
(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.
(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.
(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?
(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.
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) + "."))
(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.
Can you add ints to a string just like that? using "+"?
Cause when I tried it that didn't work.
(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.
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?
(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.
(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)
Oh you're right in that part, I thought you were talking about the actual comparing.
KK, just a misunderstanding [:
Anyways Poster read the comments and see whats wrong in your code!


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by HardStoNJul 19, 2020 05:14 AM 7 290 Jul 19, 2020 05:14 AM
Last Post: DanketyDank
[C] by Sir BillyAug 26, 2019 06:33 PM 1 149 Aug 26, 2019 06:33 PM
Last Post: YouthInAsia
[Python] by kookurMar 4, 2019 04:18 PM 5 172 Mar 4, 2019 04:18 PM
Last Post: nicodxmus