Python Forum
Why can't I get this code to loop
Thread Rating:
  • 2 Vote(s) - 4.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why can't I get this code to loop
#1
import operator
import random

casefile=open('case.txt','a')
devicefile = open('devicefile.txt','r')

def ask():

    global model
    global memory
    global devicefile
    
    deviceline = devicefile.readlines()
    print (deviceline[0]) #what model
    model = input()
    print (deviceline[1])  #memory
    memory = input()
    print (deviceline[2])  #problem

def no():

    global model
    global memory
    global casefile

    print('No solution')
    casefile.write('Your case number is {}\nYour case phone brand is {}\nYour phone model is {}\nYour phone memory is {}\nYour problem is {}'.format(casenumber,brand,model,memory,problem,))
    casefile=open('case.txt','r')
    file=casefile.readlines()

def end():
    global casefile
    global devicefile
    devicefile.close
    casefile.close
    exit()

solutionfound='no'
loopv=1

brand=input('What phone brand do you have, apple, Nokia Samsung.').lower()

#-----------------------------------------------------------------------------------#

def applei():
    global solutionfound
    problem = input()
    casenumber = random.randint(1000,2000)
    list_problem=problem.split()
    for x in range (0,len(list_problem)):
        position = operator.itemgetter(x)
        word=position(list_problem)
        
        if word == 'dropped':
            solutionfound = 'yes'
            print('This should work as a suitable solution')
            print('Bring you apple device to the nearest apple store.')
            loop()

        elif word == 'space':
            solutionfound = 'yes'
            print('This should work as a suitable solution')
            print('Your current Apple device does not have enough storage. Consider buying a new device.')

        elif word == 'slow':
            solutionfound = 'yes'
            print('This should work as a suitable solution')
            print('The current device you have will not be able to run newer and more demanding software. Consider buying a new device. ')
            
    solutionfound=='no'
    if solutionfound=='no':
        no()

def loop():
    global loopv
    loopv=1

    issue = input('Is this a suitable solution').lower()
    list_issue=issue.split()
    for x in range (0,len(list_issue)):
        position = operator.itemgetter(x)
        word=position(list_issue)

        if word =='yes':
            loopv=2
        else:
            loopv=1
            
    


#-----------------------------------------------------------------------------------#
#-----------------------------------------------------------------------------------#
#-----------------------------------------------------------------------------------#
        
if brand==('apple'):
    ask()
    while (loopv==1):
        applei()

print (loopv)
end()
Reply
#2
First, stop using global everywhere. Learn how to use function parameters and return values to pass information between functions. There is a tutorial on functions on this site: https://python-forum.io/Thread-Basic-Functions

Second, loop directly over sequences. That's what Python was built to do. This code:
    list_problem=problem.split()
    for x in range (0,len(list_problem)):
        position = operator.itemgetter(x)
        word=position(list_problem)
can be replaced by:
    for word in problem.split():
Which does not require me to twist my brain to figure out your code.

Also, loopv is a horrible variable name. It gives no indication as to what it is for. It made figuring out the problem much harder.

I thought the problem was that you only call loop for one solution (dropped). That probably is a problem. But I think a bigger problem is that you are looping over word lists to see if a word is in the list. So you change loopv for each word. So if yes is in the list, loopv gets set to 2. But if there is another word after the yes, loopv gets set to 1, effectively ignoring the yes. Get rid of that loop altogether and use the in operator:
if 'yes' in issue.split():
    loopv = 2
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

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