#🔒 NameError: name 'num1' is not defined

80 messages · Page 1 of 1 (latest)

mighty oracleBOT
#

@quartz thistle

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

quartz thistle
# mighty oracle <@893647816830889984>
   def input_numbers():
        global num1, num2
        num1 = int(input("Первое число: "))
        num2 = int(input("Второе число: "))

    print("1 уровень - Диапозон числа которого вам нужно найти равен от 1000 до 10000"
            "\nВремя на ответ дается 20 секунд"
            "\nОтвет нужно будет найти сложение"
            "\n2 уровень - Диапозон числа которого вам нужно найти равен от 100 тысяч до 1 милионна"
            "\nВремя на ответ дается 15 секунд"
            "\nОтвет нужно будет найти вычитанием"
            "\n3 уровень - Диапозон числа которого вам нужно найти равен от 1 милионна до 10 милионнов"
            "\nВремя на ответ дается 10 секунду"
            "\nОтвет нужно найти умножением")

    choise = int(input("Выберите уровень:"))

    num_to_guess = 0

    if choise == 1:
        result = num1 + num2
        num_to_guess = random.randint(1000, 10000)
        timeout = 20
        print("Сложите 2 числа, чтобы получился результат:", num_to_guess)
    elif choise == 2:
        num_to_guess = random.randint(100000, 1000000)
        timeout = 15
        result = num1 - num2
        print("Вычте 2 числа, чтобы получился результат:", num_to_guess)
    elif choise == 3:
        num_to_guess = random.randint(1000000, 10000000)
        timeout = 10
        result = num1 * num2
        print("Сложите 2 числа, чтобы получился результат:", num_to_guess)
    else:
        print("Неверный выбор. Выберите от 1 до 3.")
        exit()
    print("\nПравильный ответ!" if result == num_to_guess else "\nНеправильный ответ :(")

    num1 = int(num1)
    num2 = int(num2)
amber rampart
#

it usually points to a line that causes the error

amber rampart
#

also it would be nice to share the full code

#

!paste

mighty oracleBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

amber rampart
#

use the link in that message

amber rampart
amber rampart
#

if you want to get the result of this function, then simply do return num1, num2 at the end

quartz thistle
muted juniper
#

what error are you getting if you remove global num1, num2?

amber rampart
#
import random
import threading
import time

print("Добро пожаловать в Death Numbers, твоя задача найти правильный ответ на время, удачи!")

score = 0

while True:
    # Ваш блок кода
    def input_numbers():
        global num1, num2
#

you say global num1, num2

#

but they don't exist

#

just delete this line, first of all

#

and most importantly

#

two questions

#
  1. why you define a function over and over in a while loop?
#
while True:
    # Ваш блок кода
    def input_numbers():
        global num1, num2
        num1 = int(input("Первое число: "))
        num2 = int(input("Второе число: "))

    print("1 уровень - Диапозон числа которого вам нужно найти равен от 1000 до 10000"
            "\nВремя на ответ дается 
#

and pay attention to spaces before things

quartz thistle
amber rampart
#

but why do you have def in here?

#

def defines a function that you can call later

#

but it does not run the code inside of function

quartz thistle
amber rampart
#

thats why num1 and num2 are not defined lower

amber rampart
quartz thistle
#

bro, I'm confused, could you please completely correct the code and send me the full version via a link?👉 👈

prime agate
#

We don't do that here.

quartz thistle
prime agate
#

We'll help you try to learn but we're not going to write code for you.

#

Do you understand the difference between defining a function and calling it?

quartz thistle
prime agate
#

It's a pretty fundamental concept in Python.

#

Did you write all this code yourself or get help from like ChatGPT?

#

!res here are some great resources to learn from

mighty oracleBOT
#
Resources

The Resources page on our website contains a list of hand-selected learning resources that we regularly recommend to both beginners and experts.

prime agate
#

Many of them are free. I recommend Automate the Boring Stuff

#

Some people really like the CS50p online course too.

quartz thistle
amber rampart
#

okay im back

quartz thistle
amber rampart
#

basically, you use def to define a function that you can call later on. look at this example

def func():
    print('one thing')
    print('another thing')
#

and after you defined it

#

you can call it: <function_name>()

#

!e

def func():
    print('one thing')
    print('another thing')

func()
func()
mighty oracleBOT
#

@amber rampart :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | one thing
002 | another thing
003 | one thing
004 | another thing
amber rampart
#

and second of all u'll need a return keyword

#

!e

def func():
    return 5, 10

a, b = func()
print(a, b)
mighty oracleBOT
#

@amber rampart :white_check_mark: Your 3.12 eval job has completed with return code 0.

5 10
amber rampart
#

so basically, inside your function you will need to get rid of global num1, num2, because you dont need to use global variables in that case (and they dont even exist)

#

and after you've got the numbers just return them

#

so, like

def input_numbers():
    num1 = int(input("Первое число: "))
    num2 = int(input("Второе число: "))
    return num1, num2

num1, num2 = input_numbers()
#

and thats it

#

or you can get rid of that def completely because you dont really do anything in that function

amber rampart
#

but function will be defined outside

#

or as i said u cant get rid of defining a function

#

and just do num1 = 1 int(input(... (and same with num2 in that loop)

quartz thistle
amber rampart
#

and can u show the code u wrote in that part

#

along with function definition

quartz thistle
# amber rampart and can u show the code u wrote in that part
import random
import threading
import time

print("Добро пожаловать в Death Numbers, твоя задача найти правильный ответ на время, удачи!")

score = 0

def input_numbers():
    num1 = int(input("Первое число: "))
    num2 = int(input("Второе число: "))
    return num1, num2

num1, num2 = input_numbers()

while True:
    # Ваш блок кода


    print("1 уровень - Диапозон числа которого вам нужно найти равен от 1000 до 10000"
            "\nВремя на ответ дается 20 секунд"
            "\nОтвет нужно будет найти сложение"
            "\n2 уровень - Диапозон числа которого вам нужно найти равен от 100 тысяч до 1 милионна"
            "\nВремя на ответ дается 15 секунд"
            "\nОтвет нужно будет найти вычитанием"
            "\n3 уровень - Диапозон числа которого вам нужно найти равен от 1 милионна до 10 милионнов"
            "\nВремя на ответ дается 10 секунду"
            "\nОтвет нужно найти умножением")
...
amber rampart
#

your num1, num2 are outside of the loop

quartz thistle
quartz thistle
# amber rampart your `num1, num2` are outside of the loop
print("Добро пожаловать в Death Numbers, твоя задача найти правильный ответ на время, удачи!")

score = 0

def input_numbers():
    num1 = int(input("Первое число: "))
    num2 = int(input("Второе число: "))
    return num1, num2



while True:
    # Ваш блок кода
    num1, num2 = input_numbers()

    print("1 уровень - Диапозон числа которого вам нужно найти равен от 1000 до 10000"
            "\nВремя на ответ дается 20 секунд"
            "\nОтвет нужно будет найти сложение"
            "\n2 уровень - Диапозон числа которого вам нужно найти равен от 100 тысяч до 1 милионна"
            "\nВремя на ответ дается 15 секунд"
            "\nОтвет нужно будет найти вычитанием"
            "\n3 уровень - Диапозон числа которого вам нужно найти равен от 1 милионна до 10 милионнов"
            "\nВремя на ответ дается 10 секунду"
            "\nОтвет нужно найти умножением")
amber rampart
#

also i think print("1 уровень - Диапозон числа которого вам нужно найти равен от 1000 до 10000" that print is supposed to be at the start of a program?

#

cuz u will send that super long message every single iteration

#

and not just once

mighty oracleBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.