Hi guys i wonder if someone can help an absolute noob out with some homework.
i was given a brief of:
ask a user to input up to 3 mobile numbers.
for the numbers entered display the list of numbers entered and clearly displays the mobile network each number belongs to alongside,your program should also indicate if a number inputted does not belong to a mobile network .
using the following parameters :
prefix network
o83 3ireland
085 meteor
086 o2
087 vodafone

so far i have :

from easygui import *

# create varible to say if program runs or not
run = "yes"

#while loop to keep program running

while run == "yes":
    
#create list to hold prefix values
    networkList = ["087","086","085","083"]
    
#set prompt,title and fields

    prompt = "please enter your phone numbers "
    title = "checks which Irish phone network you are using "
    fields = ["phone number 1 ","phone number 2 ","phone number 3 "]

#get user input

    userInput =[]
    userInput = multenterbox(prompt,title,fields)
    
#assign variables to input
    
    userInput1=userInput[0]
    userInput2=userInput[1]
    userInput3=userInput[2]
    inputList=[userInput1,userInput2,userInput3]
    
#slice prefix for comparison to list
    
    prefix1=userInput1[:3]
    prefix2=userInput2[:3]
    prefix3=userInput3[:3]
    
#set title for output message
    
    title="your results " 
    msg="\n"+userInput1+ "\t"+ userInput2 +"\t"+userInput3
    
    if prefix1==networkList[0] or prefix2==networkList[0] or prefix3==networkList[0]:
        msg+= "\n"+" vodafone ""\t"
    if prefix1==networkList[1] or prefix2==networkList[1] or prefix3==networkList[1]:
        msg+= " o2 "+"\t"
    if prefix1==networkList[2] or prefix2==networkList[2] or prefix3==networkList[2]:
        msg+= " meteor "+"\t"
    if prefix1==networkList[3] or prefix2==networkList[3] or prefix3==networkList[3]:
        msg+="\t"+" 3Ireland "+"\t"
    
#run again ?
        
    msg+="\n""would you like to run this program again? "
    run= buttonbox(msg,title,choices=["yes","no"])
no = msgbox("\n""goodbye")

while it seems to work ...there is an issue with the layout and some of the numbers dont match up with the network name.

can any one steer me in the right direction before my head go boom ?

also there must be a more elegant way of coding this ...is there ?
pleases and thank yous included :)

Recommended Answers

All 7 Replies

Hey,

Here is what I would do... I've ignored the GUI stuff as I have no clue about that but as far as the actual code :

#get the numbers here
number_1 = raw_input('Please enter your first mobile number: ')
number_2 = raw_input('Please enter your second mobile number or press enter to skip: ')
number_3 = raw_input('Please enter your third mobile number or press enter to skip: ')
#

if number_1 != None: # if the number does not equal nothing
	if number_1[:3] == '083' : # checks first 3 digits of number 1 for a match
		print number_1 + ' - 3Ireland'
	elif number_1[:3] == '085' :
		print number_1 + ' - Meteor'
	elif number_1[:3] == '086' :
		print number_1 + ' - O2'
	elif number_1[:3] == '087' :
		print number_1 + ' - Vodafone'
	else : 
		print number_1 + ' - No match found'

if number_2 != None: # if the number does not equal nothing
	if number_2[:3] == '083' : # checks first 3 digits of number 2 for a match
		print number_2 + ' - 3Ireland'
	elif number_2[:3] == '085' :
		print number_2 + ' - Meteor'
	elif number_2[:3] == '086' :
		print number_2 + ' - O2'
	elif number_2[:3] == '087' :
		print number_2 + ' - Vodafone'
	else : 
		print number_2 + ' - No match found'

if number_3 != None: # if the number does not equal nothing
	if number_3[:3] == '083' : # checks first 3 digits of number 3 for a match
		print number_3 + ' - 3Ireland'
	elif number_3[:3] == '085' :
		print number_3 + ' - Meteor'
	elif number_3[:3] == '086' :
		print number_3 + ' - O2'
	elif number_3[:3] == '087' :
		print number_3 + ' - Vodafone'
	else : 
		print number_3 + ' - No match found'

Is that what you were after? I don't know if its more elegant but seems to work for me

Any questions let me know :twisted:

If you store the prefixes in a list:

prefix1=userInput1[:3]
    prefix2=userInput2[:3]
    prefix3=userInput3[:3]
##
##  becomes
     prefix_list = [prefix[:3] for prefix in userInput]

You can then clean up the if() statements:

if prefix1==networkList[0] or prefix2==networkList[0] or prefix3==networkList[0]:
        msg+= "\n"+" vodafone ""\t"
#
#    + the other if() statements becomes
message_tup = ("vodafone ", "o2 ",  "meteor ")

for x in range(3):
    if networkList[x] in prefix_list:         ## x = 0 through 2
        msg+= "\n %s\t" % (message_tup[x])   ## literal in tuple = same 0 through 2

This is the preferred way to do it as you can then easily modify the code with additions to the list instead of more if statements.

excellent ...two great answers ...thank you very much.
i was obviously not seeing the wood for the trees!!
@ brendon28 thanks but i am very poor and cant afford premium rate to answer my homework problems.

thanks again for the help guys.

You can code the not found numbers in two ways:

message_tup = ("vodafone ", "o2 ",  "meteor ")
 
found = False
for x in range(3):
    if networkList[x] in prefix_list:         ## x = 0 through 2
        msg+= "\n %s\t" % (message_tup[x]) 
        found = True

if not found:
    print "none of these were found",
    for x in range(3):
        print prefix_list[x],
    print
#
# or use a function
def find_prefix(networkList, prefix_list):
    message_tup = ("vodafone ", "o2 ",  "meteor ")
    for x in range(3):
        if networkList[x] in prefix_list:
            msg+= "\n %s\t" % (message_tup[x]) 
            return True

    print "none of these were found",
    for x in range(3):
        print prefix_list[x],
    print
    return False

Nice one woooee :cool:

I've learnt something new! :*

sorry guys, its been a busy week .
i am gonna mark this solved.
thanks for all the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.