(im noob) I'm having a problem using the keyboard module, every time I use it all the input I enter will be visible on the next input statement.
while pamatoChooser:
pamatoChooser = True
pamatoScreen = True
piliCardo()
ownedChecker()
backUseNext()
inputChooser = keyboard.read_key()
if inputChooser == ("1"):
inputPamato = ('1')
break
elif inputChooser == ("b"):
# nothing here yet
pass
elif inputChooser == ("Esc"):
bMain = False
pamatoScreen = False
break
elif inputChooser == "n":
transition1()
johnnyLoop = True
while johnnyLoop:
piliJohnny()
ownedChecker()
backUseNext()
inputChoose1 = str(input("Choose your pamato: "))
if inputChooser == ('1'):
inputPamato = ('2')
pamatoChooser = False
break
elif inputChooser == ('b'):
transition2()
break
Related
This code is showing the error. But we type it in a different way it runs. I am not getting it.
any one please help me why the second code isn't showing the error?
You can check the error by this link:
def check_input():
choice = 'wrong'
digit = False
while choice.isdigit() == False or digit == False:
choice = input('Enter the value: ')
if choice.isdigit() == False:
print('Please enter the digit between 0 to 10')
if choice.isdigit() == True:
if int(choice) < 1 and int(choice) > 10:
digit = False
else:
choice = True
return int(choice)
def check_input():
choice = 'wrong'
within_range = False
acceptable_range = range(0,10)
while choice.isdigit() == False or within_range == False:
choice = input('Enter the value: ')
if choice.isdigit() == False:
print('Please enter the digit between 0 to 10')
if choice.isdigit() == True:
if int(choice) in acceptable_range:
within_range = True
else:
within_range = False
return int(choice)
The first code has this line:
choice = True
which, when executed, makes choice a value of type bool. That's why, the next time choice.isdigit() is executed, and because bool objects really don't have an isdigit attribute, you get the error.
The second code doesn't do that, so you don't get the error.
I am trying to understand why Python is not getting into the loop and exiting with an error code.
On the same time OR condition works fine.
def user_choice():
choice=''
within_range = False
while choice.isdigit == False and within_range == False:
choice=input('Enter valid selection (1-9): ')
if choice.isdigit() == False:
print('You entered non-digit value, please input digit')
if choice.isdigit() == True:
if int(choice) in range(0,10):
within_range=True
else:
within_range=False
return int(choice)
There is also another flaw in the following blocks of code:
(you should change the "and" with "or" to get a proper result, otherwise regardless of what integer you put in( not inside the range), it will return that!
def player_choice():
choice=' '
within_range = False
while choice.isdigit() == False **or** within_range == False:
choice=input('Enter valid selection (0, 1, 2): ')
if choice.isdigit() == False:
print('You entered non-digit value, please input digit(0, 1, 2)')
if choice.isdigit() == True:
if int(choice) in range(0,3):
within_range=True
else:
within_range=False
return int(choice)
You have error in this line:
while choice.isdigit == False and within_range == False:
You are comparing function str.isdigit with boolean False
You should instead evaluate function choice.isdigit()
Full fixed version:
def user_choice():
choice = ''
within_range = False
while choice.isdigit() == False and within_range == False:
choice = input('Enter valid selection (1-9): ')
if choice.isdigit() == False:
print('You entered non-digit value, please input digit')
if choice.isdigit() == True:
if int(choice) in range(0, 10):
within_range = True
else:
within_range = False
return int(choice)
I have written a main script that has a menu with 5 different options, the fifth option is if the user wants to quit the program. If the user types 5 in the main menu the program is supposed to quit, but it doesn't... It just keeps on looping through the menu. Can anyone help me resolve this issue??
menuItems = np.array(["Load new data", "Check for data errors", "Generate plots", "Display list of grades","Quit"])
userinput = input("Please enter name of the data file: ")
grades = dataLoad(userinput)
while True:
choice = displayMenu(menuItems)
while True:
if (choice == 1):
userinput = input("Please enter name of the data file: ")
grades = dataLoad(userinput)
break
elif (choice == 2):
checkErrors(grades)
break
elif choice == 3:
gradesPlot(grades)
elif choice == 4:
show = listOfgrades(grades)
showList(show)
elif (choice == 5):
break
else:
print("Invalid input, please try again")
break
You have nested loops, and break only breaks out of the inner loop.
To remedy this delete the nested loop and the break from the else block:
while True:
choice = displayMenu(menuItems)
if (choice == 1):
userinput = input("Please enter name of the data file: ")
grades = dataLoad(userinput)
break
elif (choice == 2):
checkErrors(grades)
break
elif choice == 3:
gradesPlot(grades)
elif choice == 4:
show = listOfgrades(grades)
showList(show)
elif (choice == 5):
break
else:
print("Invalid input, please try again")
In your code, when you call break, it breaks from the inner while loop and goes to the outer while loop which causes the whole thing to go on again. If for some cases you want both of the while loops to break then you can use some sort of a flag.
Example,
flag = False
while True:
while True:
if (1 == var):
flag = True
break
if flag:
break
//Your code
flag = False
while True:
choice = displayMenu(menuItems)
while True:
if (choice == 1):
userinput = input("Please enter name of the data file: ")
grades = dataLoad(userinput)
flag = True
break
elif (choice == 2):
checkErrors(grades)
flag = True
break
elif choice == 3:
gradesPlot(grades)
elif choice == 4:
show = listOfgrades(grades)
showList(show)
elif (choice == 5):
flag = True
break
else:
print("Invalid input, please try again")
flag = True
break
if True == flag:
break
I have a small script I have been working on for practice with Python. I am having trouble getting my input to accept a number for an if statement and also accept string as lower case.
I want to tell my script for any input if the user types '99' then close the program. So far it works where I have int(input()), but it won't work where I have input(). What am I doing wrong or is it not something I can do?
Right now my if statement looks like:
if choice1 == 99:
break
Should I just make 99 into a string by quoting it?
Maybe something like this:
if choice1 == "99":
break
Here is the script:
global flip
flip = True
global prun
prun = False
def note_finder(word):
prun = True
while prun == True:
print ('\n','\n','Type one of the following keywords: ','\n','\n', keywords,)
choice2 = input('--> ').lower()
if choice2 == 'exit':
print ('Exiting Function')
prun = False
start_over(input)
elif choice2 == 99: # this is where the scrip doesnt work
break # for some reason it skips this elif
elif choice2 in notes:
print (notes[choice2],'\n')
else:
print ('\n',"Not a Keyword",'\n')
def start_over(word):
flip = True
while flip == True:
print ('# Type one of the following options:\n# 1 \n# Type "99" to exit the program')
choice1 = int(input('--> '))
if choice1 == 99:
break
elif choice1 < 1 or choice1 > 1:
print ("Not an option")
else:
flip = False
note_finder(input)
while flip == True:
print ('# Type one of the following options:\n# 1 \n# Type "99" to exit the program')
choice1 = int(input('--> '))
if choice1 == 99:
break
elif choice1 < 1 or choice1 > 1:
print ("Not an option")
else:
flip = False
note_finder(input)
So input() always returns a string. You can see the docs here:
https://docs.python.org/3/library/functions.html#input
What you could do is something like this:
choice2 = input('--> ')
if choice2.isnumeric() and (int(choice2) == 99):
break
this avoids you to type check and catch errors that aren't important.
see below for how isnumeric works with different numeric types:
In [12]: a='1'
In [13]: a.isnumeric()
Out[13]: True
In [14]: a='1.0'
In [15]: a.isnumeric()
Out[15]: False
In [16]: a='a'
In [17]: a.isnumeric()
Out[17]: False
def IsValidLogin(login_name):
lognum = 0
login = 0
if len(login_name) >= 8:
lognum == True
elif len(login_name) < 8:
lognum == False
for chr in login_name:
if chr.isalnum() == True:
login == True
else:
login == False
if (login == True) and (lognum == True):
IsValidLogin == True
else:
IsValidLogin == False
if IsValidLogin == True:
print('Login Accepted')
else:
print('Login Failed')
return lognum, login
login_name = input('Please enter your Login: ')
lognum, login = IsValidLogin(login_name)
def IsValidPassword(password):
pass_char = ['!','#','#','$','%','^','&','*','(',')','_','+']
pass_len = 0
pass_num = 0
spec_char = 0
for chr in password:
if len(password) >= 8:
pass_len == True
elif len(password) < 8:
pass_len == False
for chr in password:
if chr.isdigit() == True:
pass_num == True
elif chr.isdigit() == False:
pass_num == False
for chr in password:
if password == pass_char:
spec_char == True
elif password != pass_char:
spec_char == False
if (pass_len == True) and (spec_char == True):
IsValidPassword == True
else:
IsValidPassword == False
if IsValidPassword == True:
print('Password Accepted')
else:
print('Password Failed')
return pass_len, pass_num, spec_char
password = input('Please enter your password: ')
pass_len, pass_num, spec_char = IsValidPassword(password)
The login needs to be at least 8 characters long and can only contain letters and numbers.
The password has to be at least 8 characters long, have at least one number, and one special character.
Program runs, but both return false.
Wondering what I am doing wrong. Please keep in mind I am very new into Python.
The lines:
if len(login_name) >= 8:
lognum == True
elif len(login_name) < 8:
lognum == False
seem wrong. It seems like you're trying to assign to lognum but instead you are comparing lognum to True and False. = is assignment and == is comparison. Similar for:
if chr.isalnum() == True:
login == True
else:
login == False
and
if (login == True) and (lognum == True):
IsValidLogin == True
else:
IsValidLogin == False
and several other places outside the function.
Just change the appropriate == to = and it should work.
You also have a logic problem in the password part even with this change: this will always return false, because it only checks the last character, which cannot be both a number and a special character.
for chr in password:
if chr.isdigit() == True:
pass_num = True
elif chr.isdigit() == False:
pass_num = False #On the last pass through, this overwrites the previous value
for chr in password:
if password == pass_char:
spec_char = True
elif password != pass_char:
spec_char = False #On the last pass through, this overwrites the previous value
Change elif password != pass_char: to elif not spec_char: and elif chr.is_digit(): to elif not pass_num: to keep this from being overwritten.