How to prevent entering negative numbers in Kivymd MDTextField - python

In MDTextField, there is a restriction on entering only numbers, this is input_filter: "float", "int". But you can enter negative numbers
MDTextField:
id: z1_m
text:""
hint_text: "z1"
helper_text: "Число зубьев шестерни"
helper_text_mode: "on_focus"
input_filter: "float"
on_text_validate: app.prochnjst()
Expected that when the user enters a minus sign, nothing happens

You can use try/ except:
try:
x = int(input())
assert x > 0
except AssertionError :
print("Only positives are allowed")

Related

My code keeps reapting it self I don't how i will stop it so it can move on and do the other things i want it to do

So here is the code if run the begin function it will just run option1 and not print the other stuff like i want to make this login thing work but it won't work for some reseon.
def login():
print ("You are now logged in")
def reg():
file = open ("User_details","a")
file.write("\n"+"Ussername"+","+"Password"+","+"Age"+","+"Real_name"+","+"Last_name"+","+"Email")
file.close
def Access(option):
if option == "login":
input ("Ussername: ")
input ("Password: ")
input ("Age: ")
input ("Real name: ")
input ("Last name: ")
input ("Email: ")
login()
else:
print ("Enter all the info we will be asking you to register")
input ("Ussername: ")
input ("Password: ")
input ("Age: ")
input ("Real name: ")
input ("Last name: ")
input ("Email: ")
reg()
def begin():
print ("Hi my name is Steve would you like to register or login")
option1 = input ("Login or Register: ")
if option1 != "login" and "register":
begin()
if option1 == "register":
Access()
if option1 == "login":
login()
begin()
Access()
This is because your first conditional will always be True. For example, let's see how an example input would work:
>>> option1 = "unknown input"
>>> if option1 != "login" and "register": print("TRUE")
TRUE
# If we split the statement into pieces, we can see why
>>> option1 != "login"
True
# The part after the `and` isn't testing anything, and is always True!
>>> bool("register")
True
# Your `if` statement effectively tests option1 != "login"
>>> True and "register"
True
To fix this, you want to make sure that option1 isn't either of the strings. You probably want to clean up the input value a bit too, by stripping any whitespace.
option1 = input("Login or Register: ")
option1 = option1.strip() # remove accidental whitespace from the ends
# The fixed `if` statement
if option1 != "login" and option1 != "register":
begin()

How can I make the program go to the input?

Write a program that asks the user to put the password, first name, last name, and id. If any of that is invalid, program to continuously ask to provide the valid input. Here are the rules:
Password has to be at least 7 characters, must contain at least one uppercase letter, at least one lowercase letter, and at least 1 digit
First name and last name must contain only letters
ID must contain only digits
After you have input everything validly print them on the screen. As the password is sensitive info print only the first three characters and for the rest of the length, print '*'
password = 'Pass1234'
first_name = 'John'
last_name = 'Smith'
ID = '1234'
p = input('Input your password: ')
if (len(p)<6):
print('Invalid password. Must be at least 6 characters')
p = input('Input your password: ')
elif not p.islower():
print('Invalid password. Must have at least 1 lowercase character')
elif not p.isupper():
print('Invalid password. Must have at least 1 uppercase character')
if p == password:
print('Well done! This is a valid password')
password_list = list(password)
password_list[3] = '*'
password_list[4] = '*'
password_list[5] = '*'
password_list[6] = '*'
password_list[7] = '*'
edited_password = ''.join(password_list)
fn = input('Please enter your first name: ')
ln = input('Please enter your last name: ')
for f in fn:
if f.isdigit():
print('Invalid Name! Name should only contain letters')
fn = input('Please enter your first name: ')
for l in ln:
if l.isdigit():
print('Invalid Name! Name should only contain letters')
ln = input('Please enter your last name: ')
if fn == first_name or 'john' and ln == last_name or 'smith':
print('Well done! This is a valid name')
I = input('Please enter your ID: ')
if I.isupper():
print('Invalid ID! ID should only contain numbers')
I = input('Please enter your ID: ')
elif I.islower():
print('Invalid ID! ID should only contain numbers')
I = input('Please enter your ID: ')
elif I == ID:
('Well done! This is a valid ID')
print('Name:', first_name, last_name)
print('ID:', ID)
print('Password:', edited_password)
Output:
Input your password: Pass1234
Invalid password. Must have at least 1 lowercase character
Well done! This is a valid password
Please enter your first name: John
Please enter your last name: Smith
Well done! This is a valid name
Please enter your ID: 1234
Name: John Smith
ID: 1234
Password: Pas*****
How can I fix my program where it doesn't print the lowercase error message?
You can do something like this. The problem you had is you were checking to see if your entire password was lowercase instead of if at least 1 character was.
Of course you'll probably want to change your input variable from "p" to "password" instead of hard coding it at the top of the page.
p = input("Input your password: ")
upper = 0
lower = 0
number = 0
while True:
for x in p:
if x.isupper()==True:
upper+=1
elif x.islower()==True:
lower+=1
elif x.isalpha()==False:
number+=1
if len(p) < 7 or upper <= 0 or lower <= 0 or number <= 0:
print ('password not complex enough')
p = input('please enter password again ')
upper = 0
lower = 0
number = 0
else:
break

Error handling multiple entry boxes at one time

I am error handling the customer name, distance travelled, virus protection and wof and tune for my program. What would I put in plce of 'something' or instead of using and if and else statement to run the enter_job function when there are no errors, so when none of the code inside the if something part is relevant to the data entered?
Thank you!
if something:
if self.name_entry_var.get() == "":
self.name_entry_error.configure(text = "Enter customer name")
if self.wof_var.get() == " ":
self.wof_entry_error.configure(text = "Check YES or NO")
try:
if float(self.time_entry.get()) <= 0:
self.time_entry_error.configure(text = "Enter a positive number")
except ValueError:
self.time_entry_error.configure(text = "Enter a number")
try:
if float(self.distance_entry.get()) <= 0:
self.distance_entry_error.configure(text = "Enter a positive number")
except ValueError:
self.distance_entry_error.configure(text = "Enter a number")
else:
self.enter_job()
Edit:
If I do this then it will go straight into the enter_job function without checking the entries.
try:
self.enter_job()
except:
if self.name_entry_var.get() == "":
self.name_entry_error.configure(text = "Enter customer name")
if self.wof_var.get() == " ":
self.wof_entry_error.configure(text = "Check YES or NO")
try:
if float(self.time_entry.get()) <= 0:
self.time_entry_error.configure(text = "Enter a positive number")
except ValueError:
self.time_entry_error.configure(text = "Enter a number")
try:
if float(self.distance_entry.get()) <= 0:
self.distance_entry_error.configure(text = "Enter a positive number")
except ValueError:
self.distance_entry_error.configure(text = "Enter a number")
The error handling should be done within the enter_job() function.
something like so:
def enter_job():
if self.name_entry_var.get() == "":
self.name_entry_error.configure(text = "Enter customer name")
raise(self.name_entry_error)
if self.wof_var.get() == " ":
self.wof_entry_error.configure(text = "Check YES or NO")
raise(self.wof_entry_error)
try:
if float(self.time_entry.get()) <= 0:
self.time_entry_error.configure(text = "Enter a positive number")
raise(self.time_entry_error)
except ValueError:
self.time_entry_error.configure(text = "Enter a number")
raise(self.time_entry_error)
try:
if float(self.distance_entry.get()) <= 0:
self.distance_entry_error.configure(text = "Enter a positive number")
raise(self.distance_entry_error)
except ValueError:
self.distance_entry_error.configure(text = "Enter a number")
raise(self.distance_entry_error)
then running this line:
self.enter_job()
would return exceptions on it's own.
like so:
try:
self.enter_job()
except Exception as e:
raise(e)

Accept an input as a word and not a integer

I'm using Pycharm and
I'm new to programming and I made something using input:
try:
name = (input("Enter your name: "))
print("Hello " + name)
age = int(input("Enter your age: "))
print(age)
except ValueError:
print("Invalid Input")
I would like the "name" string to only accept words not numbers
like when I run the program usually it looks like this:
Enter your name: "name"
Hello "name"
but when I put a number into it:
Enter your name: "14"
Hello "14"
I would like it to only accept words as an answer and input an error when an integer was put
Python has raise keyword to help us raise exceptions manually.
We check if the name is a number and raise a ValueError if so:
try:
name = input("Enter your name: ")
if name.isdigit():
raise ValueError
print("Hello " + name)
age = int(input("Enter your age: "))
print(age)
except ValueError:
print("Invalid Input")
On run:
Enter your name: 14
Invalid Input

My code is not executing properly in Python

I am trying to create a simple grade program in which the user is prompted for a decimal grade and in return are given a letter grade. It just doesn't seem to work. When I place a number like .9 it gives me B. It gives me B for all inputs and when I test it with a number greater than 1, it gives me "B A D C".
try :
grade = raw_input("what is ur grade")
if grade > .8 < .9 :
print "B"
if grade > .9 < 1.0 :
print "A"
if grade > .6 < .7 :
print "D"
if grade > .7 < .8 :
print "C"
if grade < .6 :
print "F"
except :
if grade > 1.0 :
print "enter numeric value"
Please Help me understand...
from bisect import bisect
def grade(score, breakpoints=[60, 70, 80, 90], grades='FDCBA'):
i = bisect(breakpoints, score)
return grades[i]
while True:
try:
grad = float(raw_input("what is ur grade")) *100
print grade(grad)
break
except:
print 'Enter Correct value'
explantion for your code:
grade = raw_input("what is ur grade")
here your grade type is always string you need to convert it into float
if grade > .8 < .9
i think your grade between .8 and .9
it can be done
if .8<grade<.9:

Categories