1

Note: I am using Python 3.5 I just started creating a part two to a text based game I made, and here is the code I'm having trouble with:

import random

def game():
    randomIp = random.randint(10, 999)

    def tutorial():
        global randomIp

        print('Hello.')
        print(randomIp + '.' + randomIp + '.' + randomIp + '.' + randomIp)

The problem that kept coming up was:

File "C:\Users\Anony\Desktop\SICCr4k2BrokeFold\SICCr4k2Broke.py", line 18, in tutorial
  print(randomIp + '.' + randomIp + '.' + randomIp + '.' + randomIp)
NameError: name 'randomIp' is not defined

I don't know what's up. I have the global put into tutorial() and it doesn't have an error for saying randomIp isn't defined in the command global randomIP only for print(randomIp + '.' + randomIp + '.' + randomIp + '.' + randomIp). Does anyone know what the problem is? And if I wanted a different random number to be printed after each ".". What would the code be for that? I would like it so that it would print out something like 23.321.43.23. A completely different number after each period.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • A "global" is a module-level variable. You're defining it in an enclosing scope, but not module level; thus, it's **not** actually a global for you. – Charles Duffy Nov 12 '15 at 22:12
  • because `tutorial()` is inside of your `game()` module level, you dont need to use `global` keyword – R Nar Nov 12 '15 at 22:14
  • @RNar: `tutorial()` does not have `randomIp=` in it. `global randomIp` should not be used in it at all regardless of where `randomIp` or `tutorial()` are actually defined. – jfs Nov 13 '15 at 02:06
  • unrelated: to print a random IPv4, you could use `print(ipaddress.IPv4Address(random.getrandbits(32)))` – jfs Nov 13 '15 at 02:09

1 Answers1

1

You created a local variable, but then you try to access a global of the same name.

You could simply omit the global keyword.

def game():
    randomIp = ...
    def tutorial():
        print(randomIp + ...)

Note that this will only work if you don't assign randomIp inside tutorial(), otherwise you will need the nonlocal declaration:

def game():
    randomIp = ...
    def tutorial():
        nonlocal randomIp
        randomIp += 5 # counts as assignment
        print(randomIp + ...)

Also note that it's more typical in python to use .format() instead of + when working with strings...

# This works
print('{0}.{0}.{0}.{0}'.format(randomIp))

# This does not work
# print(randomIp + '.' + randomIp + '.' + randomIp + '.' + randomIp)

This is because you can't add an integer to a string in Python. In some other languages, this will result in automatic conversion. In Python, it will just result in an error.

Generating a random IP

This will generate a random IP address from a valid /8 block, skipping the 127 localhost block, multicast blocks, and whatnot. It may generate addresses which are broadcast addresses, depending on the netmask.

def randomIp():
    x = random.randint(1, 222)
    if x == 127:
        x += 1
    return '{}.{}.{}.{}'.format(
        x,
        random.randint(0, 255),
        random.randint(0, 255),
        random.randint(0, 255))

Of course, you shouldn't actually use the IP address for anything.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • If I wanted a different random number to be printed after each `"."`. What would the code be for that? I would like it so that it would print out something like 23.321.43.23. A completely different number after each period @DietrichEpp – John Antonio Anselmo Nov 12 '15 at 22:24
  • @JohnAntonioAnselmo: You'd need a function, then. – Dietrich Epp Nov 12 '15 at 22:35
  • So how would I replace `print(randomIp + '.' + randomIp + '.' + randomIp + '.' + randomIp)` with that?. Do I just call `randomIp()` whenever I need one, and it will list something like 32.34.423.34, or something like that? @DietrichEpp – John Antonio Anselmo Nov 12 '15 at 22:38
  • Yes, it will return a string like that. – Dietrich Epp Nov 12 '15 at 22:38
  • It outputs an Ip with single quotes, is there any way to take away the single quotes? @DietrichEpp – John Antonio Anselmo Nov 12 '15 at 22:45
  • It doesn't output anything, because there is no `print()` call. It *returns* a string. The string does not contain any quote marks at all. However, if you evaluate an expression in the REPL that evaluates to a string, the string will be passed to `repr()` before printing, which will add quote marks. You are probably seeing the quote marks from the REPL. – Dietrich Epp Nov 12 '15 at 22:46