Like using this to validate that an input is only alpha-numeric:
while True:
str = input('')
if str.isalnum():
break
else:
print("Please include only alpha-numeric characters.\n")
This code has worked for all instances that I have tested it in, but is this bad practice?
That's fine. Here is a note, however: you can find out if the while loop exited with a break or without one by using else:
x = 0
while x < 4:
x += 1
else:
print("no break")
# prints no break
If you break, however:
x = 0
while x < 4:
x += 1
if x == 2:
break
else:
print("no break")
# Does not print
you can abstract it further
def verified_input(prompt='',test_condition=lambda x:1,err_message="Please Enter Valid Input"):
while True:
result = input(prompt)
if test_condition(result):
return result
print( err_message )
def verified_alnum(prompt,err_message="Please enter Only alpha numerics"):
return verified_input(prompt,lambda x:x.isalnum(),err_message)
result = verified_alnum("Enter Password:","A password must be only letters and numbers")
this allows you to create any number of test conditions quickly and relatively verbosely
Related
Hello this is my first question and sorry I don't know how to phrase it,
I am trying to create a two list. The first list is for one set of codes (code1) and is validated in the VC function. The second function (VC) is to validate the second list. Both functions work well in validating the numbers. However in the third function (add_validations). In that function I want to repeat VC and VQ until the user either types in END or either of the functions return False. When true I want the inputs to be appended into the two separate lists. However, the third function not only does not append any of the list it does not identify/run either of the two functions and comes up with:
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
And I'm sorry I didn't know which code to add so I added everything (delete if not allowed sorry):
Code:
def add_validations():
code1 = []
code2 = []
VC()
VQ()
while True:
record = []
i = VC()
q = VQ()
if i != "END":
VQ()
VC()
code1.append(int(i))
code2.append(int(q))
elif VC == True and VQ() == True:
VQ()
VC()
code1.append(int(i))
code2.append(int(q))
elif VC == False and VQ() == True:
print("Invalid code")
else:
print("Invalid quantity")
return code1,code2
def VC():
i = input("What is the item code \n")
minimum = 0
maximum = 39
if i == "END":
i = "END"
return showRecord()
elif int(i) > minimum and int(i) <= maximum:
return int(i)
True
else:
False
def VQ():
q = input("What quanity \n")
minimum = 0
maximum = 49
if q == "END":
isValidCode = "END"
return showRecord()
elif int(q) > minimum and int(q) <= maximum:
True
else:
False
new in python here,
I have a code which guesses number and it works fine. but what I want to add to it is when my guess is correct count down to 0,
I have this code :
num = 3
my_guess = ""
while my_guess != num:
my_guess = int(input("enter:"))
print ("great job")
where and how to I do it (with comment line if possible please )
thanks
Hopefully this isn't too much new information at once but import random if/else continue try/raise and formatted strings are certainly worth understanding.
import random # random module for PRNG
num = random.randint(0,9) # get int 0<=i<=9
count_wins = 0 # start counter for wins at 0
count_losses = 0 # start counter for wins at 1
while count_wins <= 3: # while count_wins is less than 3
try: # If an error occurs in this try block the except will be executed
my_guess = int(input("enter:")) # cast input to int and set to my_guess
if my_guess < 0 or my_guess > 9: # raise Value Error for ints out of range
raise ValueError # Raise Value Error
continue # go back to the top of the loop
except ValueError: # specify this except block is for ValueErrors
print("invalid input") # print invalid input
if(my_guess == num): # if you guessed correctly
print("great job") # print "great job"
num = random.randint(0, 9) # pick a new number
count_wins += 1 # increment counter
else: # if you guessed incorrectly
count_losses += 1 # increment counter
print(f"Wins: {count_wins}") # print wins
print(f"Losses: {count_losses}") # print losses
this is what I was looking for , in case someone else has a similar question
num = 3
my_guess = ""
while my_guess != num:
my_guess = int(input("enter:"))
print ("great job")
while num >=0:
print (num)
num = num -1
I was trying to make it so that my program will keep asking the user to input a certain value and if the user doesn't it keeps asking until they do.
I tried to use "while" instead of "if" but I know I'm probably missing something, somewhere.
def terrain(surface):
surface = raw_input("What surface will you be driving on? ")
if surface == "ice":
u = raw_input("what is the velocity of the car in meters per second? ")
u = int(u)
if u < 0:
u = raw_input("Velocity must be greater than 0")
return
if u == 0:
u = raw_input("Velocty must be a number greater than zero")
return
a = raw_input("How quickly is the vehicle decelerating? ")
a = int(a)
if a > 0:
print ("Deceleration cannot be a positive integer")
return
else:
s1 = u**2
s2 = 2*.08*9.8
s = s1/s2
print "This is how far the vehicle will travel on ice: "
print ("The vehicle will travel %i meters before coming to a complete stop" % (s))
terrain("ice")
The problem is you are using return after checking the condition which causes the function to return None you have to use break instead of return with while loop instead of if to achieve this. A better way to validate and get data is below
class ValidationError(Exception):
pass
def validate_and_get_data_count(x):
if int(x) < 0:
raise ValidationError("Cannot be less than 0")
return int(x)
def validate_and_get_data_name(x):
if len(x) < 8:
raise ValidationError("Length of name cannot be less than 8 chars")
elif len(x) > 10:
raise ValidationError("Length of name cannot be greater than 10 chars")
return x
validators = {
"count": validate_and_get_data_count,
"name": validate_and_get_data_name
}
data = {}
params = [
("count","Please enter a count: "),
("name","Please enter a name: "),
]
for param in params:
while True:
x = input(param[1])
try:
data[param[0]] = validators[param[0]](x)
break
except ValidationError as e:
print(e)
print(data)
What the above code does is for every param in params list it runs a while loop checking for every validation condition defined in its validator if valid it breaks the while loop and proceeds to next param and repeats the same process again
So I've been working on reworking Hangman in Python and I've run into an issue.
The output isn't updating at all. It always remains this mesh of underscores, although other parts of the code, such as the number of tries decreasing or used letters being added to the used list seem to function flawlessly.
Here's the code:
# IMPORT GUARDS
from random import choice
from os import system
from time import sleep
# DECLARATIONS
wordList = ["apple", "pear"]
gameWord = choice(wordList)
strList = list(gameWord)
strOut = "_" * len(gameWord)
tries = 5
used = []
alphabet = "abcdefghijklmnopqrstuvwxyz"
while True:
system ("cls")
print (strOut + "\n")
print ("Tries Left:", str(tries))
print ("Letters Used:", used)
Ltr = input ("Letter: ")
# INPUT CHECK
if len(Ltr) != 1 and Ltr.lower() in alphabet:
print ("Input is of incorect size.")
sleep(0.5)
elif len(Ltr) == 0 and Ltr.lower() in alphabet:
print ("No input value given.")
sleep(0.5)
if len(Ltr) == 1 and Ltr.lower() not in alphabet:
print ("Invalid character input.")
sleep(0.5)
if len(Ltr) != 1 and Ltr.lower() not in alphabet:
print ("Input is both too large and contains invalid characters.")
sleep(0.5)
# CORRECT INPUT
if len(Ltr) == 1 and Ltr.lower() in alphabet:
ltrPos = ( [pos for pos, char in enumerate(gameWord) if char == Ltr.lower])
# DECLARATIONS
Counter = 0
strcounter = 0
# CHECKING THE NUM OF TIMES THE INPUT APPEARS IN THE WORD
while Counter < len(strList):
if gameWord[Counter].lower() == Ltr.lower():
strcounter += 1
Counter += 1
# INPUT DOES APPEAR
if strcounter != 0:
strcounter -= 1
strOut = list(strOut)
for i in ltrPos:
strOut[ltrPos[strcounter]] = Ltr.upper()
strcounter += 1
strOut = "".join(strOut)
# INPUT DOES NOT APPEAR AND/OR IS USED
elif strcounter == 0:
if Ltr not in used:
print ("Letter not in word.")
used.append(Ltr.lower())
tries -= 1
else:
print ("Letter Already Used.")
sleep(0.5)
# OUT OF TRIES
if tries == 0:
system("cls")
print ("Game Over. \nWord: " + gameWord)
break
# VICTORY
if "_" not in strOut:
system("cls")
print ("Congratulations!")
break
system ("pause")
All advice is appreciated. Thanks in advance.
The main problem is that you forgot to call the lower function in one place:
ltrPos = [pos for pos, char in enumerate(gameWord) if char == Ltr.lower()]
But there's more. First, you can simplify the calculation of strCounter to
strcounter = len(ltrPos)
Or just check if ltrPos: instead of if strcounter != 0:. Also, you can use else instead of elif.
The next problem is strOut[ltrPos[strcounter]]. Here, you try to access lrtPos[strcounter], which will produce an index error as you decrement strcounter just once, instead of setting it back to zero. Instead, just iterate the indices in ltrPos directly:
for i in ltrPos:
strOut[i] = Ltr.upper()
Also, note that you only add the letter to the used list if it is not in the word.
IDnum = input("\nprompt: ")
if int(IDnum) >= 0 :
if int(IDnum) in T.keys() :
print("ID number(s) that {} will contact is(are) {}.".format(int(IDnum),T[int(IDnum)]))
else :
print("Entered ID number {} does not exist.".format(int(IDnum)))
else:
break
It's actually a while loop, receiving ID numbers and checking whether the numbers are in the file.
I'd like to make it discern whether the input is an integer >= 0 and if it's anything else, (eg. space,enter,characters,float,etc) break the loop.
How can I do this using if statements?
I have tried
if IDnum == '' or IDnum == ' ' or int(IDnum) < 0 :
but as you know, it cannot cover all the other cases.
T = {1: 1, 2: 2}
while True:
IDnum = input("\nprompt: ")
try:
num = int(IDnum)
if num < 0:
raise ValueError('Negative Integers not allowed')
except ValueError: # parsing a non-integer will result in exception
print("{} is not a valid positive integer.".format(IDnum))
break
if num in T:
print("ID number(s) that {} will contact is(are) {}.".format(num,T[num]))
else:
print("Entered ID number {} does not exist.".format(num))
Thanks to #adirio and #moses-koledoye for the suggested improvements.
Do the check with a try-except statement.
def is_pos_int(IDnum):
''' Check if string contains non-negative integer '''
try:
number = int(IDnum)
except ValueError:
return False
if number >= 0:
return True
else:
return False
For example
is_pos_int('1 ') # notice the space
Out[12]: True
is_pos_int('-1')
Out[13]: False
is_pos_int('1.0')
Out[15]: False
is_pos_int('word')
Out[16]: False
Then:
while True:
if not is_pos_int(IDnum):
break
else:
val = int(IDnum)
if val in T.keys() :
print("ID number(s) that {} will contact is(are) {}.".format(val, T[val]))
else :
print("Entered ID number {} does not exist.".format(val))