I`ve been working on my code and a few warnings have popped up suach as "Name 'x' can be undefined" and "Global variable 'x' is undefined at the module level"
Quickly about what a program does: It can log in the users It can sigh up a user if there is not a one with this name A user can withydraw or deposit money to his account
Can anybody quickly go through my code and tell me what the problem is? And if there is any obvious mistake, please, let me know. Would be very grateful.
users_info = {
'Bill_Cruz': {
'age': 45,
'number': ' 248-4052-388',
'card': '5269-5440-9962-2708',
'balance': 4321.54,
'password': 'bill'
},
'Robert_Soup': {
'age': 34,
'number': ' 562-5065-778',
'card': '4716-7816-7850-1836',
'balance': 12701,
'password': 'robertsoup123'
},
'Paula_Smith': {
'age': 41,
'number': ' 231-7307-367',
'card': '4556-4566-2049-4595',
'balance': 75.65,
'password': 'paulasmith123'
}
}
def name_validation():
global name, name_edited **### line 27**
while True:
name = input('Please give me your name and surname: ').title().strip()
name_edited = name.replace(" ", "_")
if name.lower().strip() == "r":
print('Wellcome to the registration page!')
new_name = input('I`m gonna need your name and surname: ').title().strip()
new_name_edited = new_name.strip().replace(" ", "_")
users_info.update({new_name_edited: {'password': input('Enter a new password: ').strip(),
'balance': 0}})
name = new_name
name_edited = new_name_edited
break
elif name_edited not in users_info.keys():
print('USER NOT FOUND, try again: (r - for signing up)')
continue
else:
break
def log_in():
attempts = 3
while True:
if attempts != 0:
password = input(f'Please enter your password\n(attempts left - {attempts}): ').strip()
if password == users_info[name_edited]['password']:
print('>>>>>>>>>>>>>>><<<<<<<<<<<<<<<')
print('You`ve successfully logged in!')
break
else:
print('The password you`ve entered is incorrect')
attempts -= 1
continue
else:
print('You have 0 attempts left \nBye')
quit()
break
def balance_interaction():
new_balance = None
while True:
print('Would you like to interact?')
user_action = input('("d" for deposit; "w" for withdrawal; "q" to quit): ')
if user_action == "d":
while True:
try:
while True: # checking if dep ammount is valid
deposit = float(input('How much money would you like to deposit?: '))
if deposit >= 0:
new_balance = users_info[name_edited]['balance'] deposit
break
else:
print('Cannot ne negative!')
continue
break
except ValueError:
print()
print('Enter an amount of money you`d like to deposit!')
continue
break
elif user_action == "w":
while True:
try:
while True:
withdraw = float(input('How much money would you like to withdraw?: '))
if withdraw >= 0:
if users_info[name_edited]['balance'] - withdraw >= 0:
new_balance = users_info[name_edited]['balance'] - withdraw
break
else:
print(f'Not enough money on the account. '
f'Your available balance is {users_info[name_edited]["balance"]}')
continue
else:
print('Cannot ne negative!')
continue
break
except ValueError:
print()
print('Enter an amount of money you`d like to withdraw!')
continue
break
elif user_action == "q":
print('Bye!')
quit()
else:
print('Enter something valid!')
continue
print(f'Your new available balance is $ {round(new_balance, 2)}')
name_validation()
print(f'Hello, {name}') **### line 120**
log_in()
balance = users_info[name_edited]['balance'] **### line 122**
print(f'Your balance is $ {round(balance, 2)}')
balance_interaction()
There is the screenshot of errors
Obviously the problem is in globalisation the variables, but I can`t figure out what with exactly.
CodePudding user response:
As a programmer you should try very, very, very hard not to use globals.
In this case it is not difficult. Just pass name
and name_edited
in and out of functions as needed.
users_info = {
'Bill_Cruz': {
'age': 45,
'number': ' 248-4052-388',
'card': '5269-5440-9962-2708',
'balance': 4321.54,
'password': 'bill'
},
'Robert_Soup': {
'age': 34,
'number': ' 562-5065-778',
'card': '4716-7816-7850-1836',
'balance': 12701,
'password': 'robertsoup123'
},
'Paula_Smith': {
'age': 41,
'number': ' 231-7307-367',
'card': '4556-4566-2049-4595',
'balance': 75.65,
'password': 'paulasmith123'
}
}
def name_validation():
while True:
name = input('Please give me your name and surname: ').title().strip()
name_edited = name.replace(" ", "_")
if name.lower().strip() == "r":
print('Wellcome to the registration page!')
new_name = input('I`m gonna need your name and surname: ').title().strip()
new_name_edited = new_name.strip().replace(" ", "_")
users_info.update({new_name_edited: {'password': input('Enter a new password: ').strip(),
'balance': 0}})
name = new_name
name_edited = new_name_edited
elif name_edited not in users_info.keys():
print('USER NOT FOUND, try again: (r - for signing up)')
continue
return name, name_edited
def log_in(name_edited):
attempts = 3
while True:
if attempts != 0:
password = input(f'Please enter your password\n(attempts left - {attempts}): ').strip()
if password == users_info[name_edited]['password']:
print('>>>>>>>>>>>>>>><<<<<<<<<<<<<<<')
print('You`ve successfully logged in!')
break
else:
print('The password you`ve entered is incorrect')
attempts -= 1
continue
else:
print('You have 0 attempts left \nBye')
quit()
break
def balance_interaction(name_edited):
new_balance = None
while True:
print('Would you like to interact?')
user_action = input('("d" for deposit; "w" for withdrawal; "q" to quit): ')
if user_action == "d":
while True:
try:
while True: # checking if dep ammount is valid
deposit = float(input('How much money would you like to deposit?: '))
if deposit >= 0:
new_balance = users_info[name_edited]['balance'] deposit
break
else:
print('Cannot ne negative!')
continue
break
except ValueError:
print()
print('Enter an amount of money you`d like to deposit!')
continue
break
elif user_action == "w":
while True:
try:
while True:
withdraw = float(input('How much money would you like to withdraw?: '))
if withdraw >= 0:
if users_info[name_edited]['balance'] - withdraw >= 0:
new_balance = users_info[name_edited]['balance'] - withdraw
break
else:
print(f'Not enough money on the account. '
f'Your available balance is {users_info[name_edited]["balance"]}')
continue
else:
print('Cannot ne negative!')
continue
break
except ValueError:
print()
print('Enter an amount of money you`d like to withdraw!')
continue
break
elif user_action == "q":
print('Bye!')
quit()
else:
print('Enter something valid!')
continue
print(f'Your new available balance is $ {round(new_balance, 2)}')
name, name_edited = name_validation()
print(f'Hello, {name}')
log_in(name_edited)
balance = users_info[name_edited]['balance']
print(f'Your balance is $ {round(balance, 2)}')
balance_interaction(name_edited)
CodePudding user response:
Those messages are warning you that the global variables name
and name_edited
are defined only by running the name_validation()
function. If you hadn't run that function, those variables would not exist.
It's safer practice to define those variables at the top of the module, so they definitely exist in the global scope, without depending on running that function.