Reputation: 71
So I have a multilayered problem. I am trying to I am trying to just print out 'input was jack' if the raw_input was 'jack' and also subtract 'tries' by 1, and then run the function again and print out amount of 'tries' at the end of both the first 'if' statement and the 'else' statement, and if the raw_input wasn't 'jack' it adds 1 to 'tries' and if 'tries' reaches a total of 4 the function prints stop. I have run into a couple of different problems. Either the counter seems to reset every single time so it's always just '1' or I can't just keep running the function. It just doesn't seem like it's following the 'if or else' theory I thought it would be. So I have the following code:
tries = 0
def usrinfo():
name = raw_input('input name ')
a = ('jill','jack',name,'meg')
for element in a:
if element == 'jack':
print('input was jack')
tries =-1
print(tries)
usrinfo()
else:
if element != 'jack':
tries =+ 1
return tries
print(tries)
usrinfo()
if tries == 4:
print('stopped')
usrinfo()
If I type 'jack' I get:
Nothing...as in a blank output
I want:
input was jack
-1
Then I want the function to run again...and get
input was jack
-2
And so on.
If I type anything other than 'jack' I get:
Nothing...as in a blank output
I want to get:
1
Then for the function to run again....and get
2
Then if I type anything other than 'jack'. I want with each attempt to print the increasing level of tries from 1 - 4 such as below:
1
2
3
4
Then when it reaches 4 it prints 'stopped' or runs whatever command. I want to essentially keep the code running and control the value of 'tries' according to whether or not the input is 'jack' or something else. Because even when I create an even simpler code like this:
def userinput():
tries = 0
username = raw_input('input username ')
if username == 'jack':
tries -=1
print(username + ' '+ str(tries) + ' success')
userinput()
else:
if username != 'jack':
tries +=1
print('tries ' + str(tries))
userinput()
userinput()
My results on this code with input of 'f' are:
input username f
tries 1
input username f
tries 1
input username f
Instead I want to get:
input username f
tries 1
input username f
tries 2
input username f
tries 3
And with input of 'jack' I want:
input username jack
jack -1 success
input username jack
jack -2 success
input username jack
jack -3 success
input username jack
jack -4 success
And so on. Any help is much appreciated. I'm not sure what I'm doing wrong. It just seems like 'tries' is constantly resetting itself.
Hopefully this is clear...I've spent a lot of time trying to make code just to be able to demonstrate to someone with more knowledge where my misunderstanding might be...
Thanks guys and gals.
Upvotes: 0
Views: 131
Reputation: 640
When you are taking user input it is stored in the name variable so you need to do if with 'name' not 'element'. Secondly call the function after you have checked the condition
if tries == 4:
print('stopped')
return
usrinfo()
Here is the correct code:
tries = 0
def usrinfo():
global tries
name = raw_input('input name ')
a = ('jill','jack','meg')
#to check the input against the strings of list a go for
#if name in a:
if name == 'jack':
print('input was jack')
tries =-1
print(tries)
usrinfo()
else:
tries+=1
print(tries)
if tries == 4:
print('stopped')
return
usrinfo()
usrinfo()
Upvotes: 0
Reputation: 858
Here's what I came up with, no loops required
#!python3
from os import sys
def usrinfo(tries):
name = input('input name: ')
if tries == 4:
print('end of program because tries equals 4')
sys.exit()
elif name == 'jack':
tries -= 1
print('input was jack')
print('tries ' + str(tries) + '\n')
usrinfo(tries)
elif name != 'jack':
tries += 1
print('tries ' + str(tries) + '\n')
usrinfo(tries)
usrinfo(0) # 'tries' will be initialized to zero
Upvotes: 0