Python Forum
I'm getting an error, please help! thanks
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I'm getting an error, please help! thanks
#1
The problem is that I want to set a default value to the crate_num's and if they receive a crate, it adds one to the default value. In def start() you can see how I'm attempting to add a value to the default value, but it gives me an error. I don't want to set the value to 1, I want to add 1 to it, so if I have 2 crates and I receive another one, I then have 3 crates. thanks

ERROR:
[pyflakes] local variable 'starting_crate_num' defined in enclosing scope on line 5 referenced before assignment
[pyflakes] local variable 'starting_crate_num' is assigned to but never used

import os
from time import sleep
import random

starting_crate_num = 0
basic_crate_num = 0
rare_crate_num = 0
ultra_rare_crate_num = 0
epic_crate_num = 0

starting_crate = ["basic sword", "basic shovel", "basic rock"]
starting_random = random.choice(starting_crate)

print('before we begin, please enter you name')
name = input('> ')
money = 0
player = {"name": name, }
os.system('clear')

def start():
	os.system('clear')
	print('..You have started the game..')
	sleep(1.5)
	os.system('clear')
	print("To start you off, you're given a basic crate!")
	starting_crate_num += 1
	print('Would you like to open your crate?')
	answer = input("> ")

	if answer == "1":
		open_crate()
	elif answer == "2":
		print('')
	else:
		print()

def open_crate():
	print('...Available crates...')
	if starting_crate_num > 0:
		print(starting_crate_num)
	else:
		pass

	if basic_crate_num > 0:
		print(basic_crate_num)
	else:
		pass

	if rare_crate_num > 0:
		print(rare_crate_num)
	else:
		pass

	if ultra_rare_crate_num > 0:
		print(ultra_rare_crate_num)
	else:
		pass

	if epic_crate_num > 0:
		print(epic_crate_num)
	else:
		pass


def welcome():
	os.system('clear')
	print("Welcome to Crates, " + name + '!')
	print("(1) Start")
	print("(2) Info")
	answer = input("> ")

	if answer == "1":
		start()
	if answer == "2":
		info()
	

def info():
	os.system('clear')
	print("...Welcome to How To Play!...")
	print("""To play, you have to go on quests, while on quests,
you have a chance to find a crate. but, you also have a chance
of finding an enemy. If you don't have a weapon that is capable
of defeating that enemy, you die and lose all items you own.
You can decide the levels of quest you want to go on, and the more You
progress, the harder difficulty of quest you can go on. The higher level
your character is, you can complete quests faster, and you become stronger.
If you find a crate, you can find random items inside, depending on the quest,
and what level of crate it is, you can get better or worse items.""")
	print('------------------------------------------')
	print("Please type '1' to go back to the main menu!")
	start = input('> ')

	if start == "1":
		welcome()
	else:
		print("Invalid, please type '1'")
		info()




welcome()
Reply
#2
If you want to reassign the 'starting_crate_num' variable, you need to specify that you want to use the variable from outside the function.
You do this by writing the variable name in the parentheses.
starting_crate_num = 0

def start(starting_crate_num):
    starting_crate_num += 1
Reply
#3
Thanks for the reply, I added starting_crate_num to def start() and now it gives me another error.

Error:
Traceback (most recent call last):
File "main.py", line 103, in <module>
welcome()
File "main.py", line 73, in welcome
start()TypeError: start() missing 1 required positional argument: 'starting_crate_num'

starting_crate_numer = 0

def start(starting_crate_num):
	starting_crate_num += 1
	os.system('clear')
	print('..You have started the game..')
	sleep(1.5)
	os.system('clear')
	print("To start you off, you're given a basic crate!")
	print('Would you like to open your crate?')
	answer = input("> ")
Reply
#4
I forgot to add, when you call on your function you also have to add the 'starting_crate_num' variable name.
start(starting_crate_num)
Reply
#5
Thanks a million!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020