#🔒 python program help

41 messages · Page 1 of 1 (latest)

stark gate
#

can someone help me with this please? i want to take away 1 from guesses when i dont guess the password but i dont know why. i know this program can look stupid but i am only begginer.

import random
import time
files = 3
files_hacked = 0


password_num1 = random.randint(1,10)
password_num2 = random.randint(1,10)
guesses = 10



def password_guess1():
    print("you need to steal 3 files!")
    time.sleep(1)
    print("every file has a password!")
    time.sleep(1)
    print("you have only 10 guesses!")
    time.sleep(1)
    print("password has 2 numbers!")
    time.sleep(1)
    print("every number is in range 1-10!")
    time.sleep(1)
    user_password_input = input("Hello! to acces the first file, type password: ")
    print()
    if user_password_input != password_num1 or password_num2:
        print("none of numbers are correct!")
        
        password_wrong_guess()
    


def password_wrong_guess():
    print("Wrong password! only " + str(int(guesses)) + " guesses left!")


password_guess1()
unkempt relicBOT
#

@stark gate

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.

wet quartz
#

Are you just asking how to subtract from a variable? That can be done with -=.

#

guesses -= 1

stark gate
#

im gonna try that

wet quartz
#

Or guesses = guesses - 1, which the above is shorthand for.

#

(Prefer the first way. I just thought it would be useful to show both)

stark gate
wet quartz
#

You'll need to show the error.

stark gate
#

ok

wet quartz
#

Is it an Unbound Local error?

stark gate
wet quartz
#

You cut off the error message.

stark gate
#

sorry

wet quartz
#

Please post the full error as text.

stark gate
#

okey

#

File "c:\Users\Reviláková\Desktop\Programy\python_moje_programy\main.py", line 28, in password_guess1
guesses -= 1
^^^^^^^
UnboundLocalError: cannot access local variable 'guesses' where it is not associated with a value

wet quartz
#

Ya, so an Unbound Local. If you assign (=) a global variable inside of a function, you need a global statement to tell Python that it's a global.

#

Put global guesses as the first line in the function.

stark gate
#

okey

#

this is in problems

stark gate
wet quartz
#

No, global guesses is its own, separate line. The -= is separate.

stark gate
#

😮 its working!!!!

wet quartz
#

Put the global statement as the first line of the function, then the -= wherever in the function

#

Yay

stark gate
#

thank you !

wet quartz
#

You're welcome.

stark gate
#

sorry i know it was only one line but im learning how to code

wet quartz
#

Don't worry, this is a common issue. We get this question multiple times per day.

#

Well, the second issue at least. If you don't know how to subtract, that suggests that you skipped some early lessons.

stark gate
#

my friend from school teaching me python. but now we are not doing python but i want to do some programs so i dont forget python. im gonna look for some tutorials on youtube

#

we are doing construct 3 now

wet quartz
#

I wouldn't recommend Youtube for most learning. Use the official docs and good sites like Real Python.

stark gate
#

okey. thanks!

wet quartz
#

You're welcome.

stark gate
#

!close

unkempt relicBOT
#
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.