Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
User functions don't work:
#1
I've created a small program which runs like it should but I can't seperate the functions out and get them to work properly. Can the functions getpin(), get menuselection() be sorted out in a specific way so the program runs as normal.

This is running off a seperate class called userdetails


from Details import userdetails


def getpin():

    pin_no = ('1111', '2222', '3333')
    
    while True:

        pin_no = input("Input the no : ")

        if pin_no == '1111' or pin_no == '2222' or pin_no == '3333':
        
            
            print ("\n Hello and welcome to my program.  Please choose from one of the following options:")
        
            break
    
        else:
    
            print ("please try again ")
            
        
    
    
    Tom_Watts = userdetails ('Tom Watts \n' , '\nSidney Stringer School \n\n' 'GCSE English: Grade A \n'  'GCSE Maths:   Grade B \n' 'GCSE Physics: Grade A \n', 'GSA LTD', 'Barclays Bank', 'Good')

    Bill_Gates = userdetails ('Bill Gates \n ' '81 Oliver St \n ' 'dfdf', '6 gcses', 'CV6 7FR', 'aaa', 'good' )

    Steve_McQueen = userdetails ('Steve McQueen \n', '\nArizona High School \n\n' 'GCSE English: Grade A \n' 'GCSE Maths:   Grade A \n', 'GSA LTD', 'Lloyds Bank ', 'Average')


    dict = {
    
        '1111': Tom_Watts,
        '2222': Bill_Gates,
        '3333': Steve_McQueen, 
      
        }
    
 def getmenuselection():
    
    user =  input("\n\n 1. Userdetails \n 2. Education \n 3. Work History \n 4. Bank History \n" ' 5. Credit History \n\n  '  )

    a = '1'
    b = '2'
    c = '3'
    d = '4'
    e = '5' 

    
    if user == a:

        print (dict[pin_no].Userdetails)
    
    elif user == b:
    
        print (dict[pin_no].Education)
     
    elif user == c:
    
        print (dict[pin_no].Work_history)
    
    elif user == d:
    
        print (dict[pin_no].Bank_history)
    
    elif user == e:
    
        print (dict[pin_no].Credit_history) 


getpin()
getmenuselection()
Reply
#2
There's quite a bit of code there and you haven't really explained how the program isn't working. Are there error messages? If so, what are they? Does something else unexpected happen? What? Give us more info.
Reply
#3
1. When running the program I first input the specific pin_no and this is accepted

2. I then get the second menu -
user = input("\n\n 1. Userdetails \n 2. Education \n 3. Work History \n 4. Bank History \n" ' 5. Credit History \n\n ' )

3. When I choose lets say type '1' for Userdetails, it should link the pin_no to the dictionary, and the dictionary should link to the userdetails object with the data above.

I got this error message:-

Error:
getmenuselection() File "C:\Thonny\PythonProgs\class\New folder\education.py", line 55, in getmenuselection print (dict[pin_no].Userdetails) NameError: name 'pin_no' is not defined
Reply
#4
Do you understand what is meant by "scope"? You should go and learn about it if not.

There is no variable called pin_no in the scope of that function, hence the exception. In fact your variable called dict will have the same problem.

As an aside, dict is a poor name for a variable. Use meaningful names: what is it for? What does it contain? Also, another reason to not use the names of built in things in your programs is that you're overwriting them. That may cause problems later on ib your program.
Reply
#5
OK thanks the program runs completely fine without any functions in there, so this is why it is not
possible when adding getpin() and getmenuselection()

I think I understand what you are saying,
Reply
#6
(Aug-17-2020, 05:50 PM)Baldev10 Wrote: OK thanks the program runs completely fine without any functions in there, so this is why it is not
possible when adding getpin() and getmenuselection()
It is always possible, and usually easier, to write code that uses functions to do the same thing as code that does not use functions. However functions are not magic. Using functions or not it is still important to use a good design. I don't think the code below is a particularly good design, but it does mimic your code, uses functions, and demonstrates how to pass information to functions and return information from functions.
from Details import userdetails

users = {
    '1111' : userdetails('Tom Watts \n' , ...),
    '2222' : userdetails('Bill Gates \n ', ...),
    '3333' : userdetails('Steve McQueen \n', ...) }

def getuser(users):
    print('Select a user: ')
    for pin_no, details in users.items()
        print(pin_no, detals.Userdetails)

    while True:
        pin_no = input("Input the no : ")
        if pin_no in users:
            return users[pin_no]
        print("Please enter a number from the list ")
     
def printuserinfo(user):
    print ("What do you want to know about", user.Userdetails)
    detail =  input("1. Userdetails \n 2. Education \n 3. Work History \n 4. Bank History \n" ' 5. Credit History \n\n  '  )
    if detail == '1':
        print (user.Userdetails)
    elif detail == '2':
        print (user.Education)
    elif detail == '3':
        print (user.Work_history)
    elif detail == '4':
        print (user.Bank_history)
    elif detail == '5':
        print (user.Credit_history) 
  
user = getuser(users)
printuserinfo(user)
Reply
#7
Thanks for your efforts. Can you give a description on how each function works?

for e.g. def getuser(users):
print('Select a user: ')
for pin_no, details in users.items()
print(pin_no, detals.Userdetails)

What is happening on line 3 and 4.
Also If you look at my code above, is it possible to use global variables instead of local variables and would this work.
Reply
#8
This talks about for loops:
https://docs.python.org/3.8/reference/co...-statement

This talks about dictionaries.
https://docs.python.org/3/library/stdtyp...types-dict

Global variables should be uses sparingly if at all. The only candidate for a global variable in my example is the users dictionary and I still passed that as an argument to getuser(users) so that anyone looking at getuser would know that users is an argument that is passed to the function.

In your example you could use global variables and get it closer to working, but it would be ugly. User's of your code would wonder why you made pin_no a global when the only reason for it to exist is to select a user. Making dict a global wouldn't be a terrible design decision, but you need to change the name. Don't name variables after what type they are, name them after what they represent. And do not use names that are common python words. dict is a Python function that makes a dictionary:
x = dict(name='John Smith', age=41, occupation='Engineer')
print(x)
dict = {'name':'Jane Doe', 'age':26, 'occupation':'Engineer'}
print(dict)
y = dict(name='John Doe', age=32, occupation='Engineer')
print(y)
Output:
{'name': 'John Smith', 'age': 41, 'occupation': 'Engineer'} {'name': 'Jane Doe', 'age': 26, 'occupation': 'Engineer'} Traceback (most recent call last): File "C:\Users\djhys\Documents\python\musings\junk.py", line 5, in <module> y = dict(name='John Doe', age=32, occupation='Engineer') TypeError: 'dict' object is not callable
When "dict" was used as a variable name in line 3 it added "dict" to the local namespace. The next time "dict" is used, this time as a function, it finds the variable in the local namespace and tries to treat that as a function. I see this same thing happen with list and set quite often.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Definitions in User-Created Functions and For Loops new_coder_231013 6 2,107 Dec-29-2021, 05:51 AM
Last Post: ndc85430
  Running scripts and location of saved interpreted user-defined classes and functions leodavinci1990 3 2,542 Aug-25-2020, 03:43 AM
Last Post: micseydel
  User defined functions inside other user defined functions WildP 1 1,955 Jan-29-2020, 04:57 PM
Last Post: Clunk_Head
  Parenthesis in User-Defined Functions giorgitsu 2 1,984 Aug-07-2019, 12:56 PM
Last Post: ThomasL
  Can't work out parameter format for LibVLC functions Domarius 10 5,981 Apr-30-2019, 10:54 PM
Last Post: Domarius
  Why won't this user created function work? Evyeniarocks 5 3,139 Mar-29-2018, 05:20 PM
Last Post: wavic
  I writte 5 functions (def) but on 6 don't work. Someone can help me? perrud 2 2,803 Feb-03-2018, 10:20 AM
Last Post: perrud

Forum Jump:

User Panel Messages

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