Python - How to count how many If/Else statements exist - python

I want to count how many If/Else statements are in my function.
My code looks like this:
def countdown(type):
if type == 1:
//code
elif type == 2:
//code
else:
print(f"You have reached the end of the script. "
f"The maximum type of countdowns are: {x}")
exit(1)
Where the x is, there should be the number of if queries (If/Else). In this case, there are 3 queries. It should serve that if I create another if/else query within this function, I don't have to change the warning at the bottom of the script.
Is that even possible?
I'm using Python 3.10

Don't use if..else, but a dict or list:
types = {
1: ...,
2: ...
}
try:
types[type]
except KeyError:
print(f"You have reached the end of the script. "
f"The maximum type of countdowns are: {len(types)}")
exit(1)
What exactly to put into the dict as values depends… Can you generalise the algorithm so you just need to put a value into the dict instead of actual code? Great. Otherwise, put functions into the dict:
types = {1: lambda: ..., 2: some_func, 3: self.some_method}
...
types[type]()

Since you are using Python 3.10 you can use a new match operator. An example:
def countdown(type):
match type:
case 1:
# code
case 2:
# code
case _:
print(f"You have reached the end of the script. "
f"The maximum type of countdowns are: {x}")
exit(1)
As for me, it is a more readable solution than a dict one.
What about counting the number of options, let's consider that we have n different and logically separated options. In this case, I'd advise you an enum:
from enum import IntEnum
class CountdownOption(IntEnum):
FIRST = 1
SECOND = 2
# ...
# ...
def countdown(type):
match type:
case CountdownOption.FIRST:
# code
case CountdownOption.SECOND:
# code
case _:
print(f"You have reached the end of the script. "
f"The maximum type of countdowns are: {len(CountdownOption)}")
exit(1)

Related

Handling input data quality with checks & default value

I am trying to write a code for squaring the user input number in Python. I've created function my1() ...
What I want to do is to make Python to take user input of a number and square it but if user added no value it gives a print statement and by default give the square of a default number for e.g 2
Here is what I've tried so far
def my1(a=4):
if my1() is None:
print('You have not entered anything')
else:
b=a**2
print (b)
my1(input("Enter a Number"))
This is a better solution:
def my1(a=4):
if not a:
return 'You have not entered anything'
else:
try:
return int(a)**2
except ValueError:
return 'Invalid input provided'
my1(input("Enter a Number"))
Explanation
Have your function return values, instead of simply printing. This is good practice.
Use if not a to test if your string is empty. This is a Pythonic idiom.
Convert your input string to numeric data, e.g. via int.
Catch ValueError and return an appropriate message in case the user input is invalid.
You're getting an infinite loop by calling my1() within my1(). I would make the following edits:
def my1(a):
if a is '':
print('You have not entered anything')
else:
b=int(a)**2
print (b)
my1(input("Enter a Number"))
When I read your code, I can see that you are very confused about what you are writing. Try to organize your mind around the tasks you'll need to perform. Here, you want to :
Receive your user inputs.
Compute the data.
Print accordingly.
First, take your input.
user_choice = input("Enter a number :")
Then, compute the data you received.
my1(user_choice)
You want your function, as of now, to print an error message if your type data is not good, else print the squared number.
def my1(user_choice): # Always give meaning to the name of your variables.
if not user_choice:
print 'Error'
else:
print user_choice ** 2
Here, you are basically saying "If my user_choice doesn't exists...". Meaning it equals False (it is a bit more complicated than this, but in short, you need to remember this). An empty string doesn't contain anything for instance. The other choice, else, is if you handled your error case, then your input must be right, so you compute your data accordingly.
In your second line, it should be
if a is None:
I think what you want to do is something like the following:
def m1(user_input=None):
if user_input is None or isinstance(user_input, int):
print("Input error!")
return 4
else:
return int(user_input)**2
print(my1(input("Input a number")))

While Loop Break usage

I am totally going insane over this function I wrote, the while loop wont work for the love of god
while (something something):
print("at anytime enter 0 for both origin and destination to exit the game")
o=input("Enter origin stool's index (Must be int) ")
d=input("Enter destination stool's index (Must be int) ")
if isinstance(o,int) and isinstance(d,int):#if input d,o are int
#do bla bla
elif o==0 and d==0:#to exit manually
print("Exiting Manually...")
break
elif not (isinstance(o,int) and isinstance(d,int)):
print ("Invalid entry, exiting...")
break
The code should exit when o AND d are 0, and it should also exit when o OR d is not an int
However it seems that it doesn't matter what I enter, (i tried 0,0 and 0,1) it returns invalid entry, exiting...
Whats wrong with the conditions?
In Python 3, the input function always returns a string. It doesn't eval the user input like it used to do in Python 2. As a result, o and d are both strings, not integers. You need to wrap your input calls with int(input('...')) in order to turn them into ints, so your conditionals have a chance of success.
In this case, if the user inputs values that aren't numbers, the int call should fail with a ValueError. The standard way to deal with this is to wrap any calls to user input with a try-except block, to deal with users who provide bad input. So the relevant part of your code might look something like this:
try:
o=int(input("Enter origin stool's index (Must be int) "))
d=int(input("Enter destination stool's index (Must be int) "))
except ValueError:
print("Invalid entry, exiting...")
break
As a side note, your logic in the conditionals is kind of weird/broken. Your first conditional checks whether a and d are both ints, and then does processing. Then your next conditional (the first elif block) tries to compare those to 0, which will always fail since if you get to that test, that means the first conditional failed, so they weren't both ints and therefore can never be equal to 0.
Your first "if" will pass when o and d are ints regardless of their values. Only if that condition fails will it continue through to the next "elif".
Try something more like:
if isinstance(o,int) and isinstance(d,int) and (o != 0 or d != 0):
#Conditions met, carry on
else:
break

Python define and call function - method behavior changing upon processing user input

I defined a method, like so:
class MyDatastructure(object):
# init method here
def appending(self, elem):
self.data.append(elem)
if self.count >= self.size:
print "popping " + str(self.data[0])
print "inserting " + str(elem)
self.data.pop(0)
elif self.count < self.size:
self.count += 1
print "count after end of method " + str(self.count)
I tested it out, and it worked as supposed to.
Underneath this definition, I wanted to process some user input and use this method. However, it doesn't enter the if case anymore! Any idea why?
# in the same file
def process_input():
while True:
# getting user input
x = raw_input()
ds = MyDatastructure(x) # creating data structure of size, which was taken from user input, count initially 0
ds.appending(1)
ds.appending(2)
ds.appending(3)
# Still appending and NOT popping, even though the if in appending doesn't allow it!
# This functionality works if I test it without user input!
The problem is with this line:
x = raw_input()
Calling raw_input will return a string. If I type in the number 3, that means that the data structure will assign the string "3" to the size.
Attempting to compare a string to a number is considered undefined behavior, and will do weird things -- see this StackOverflow answer. Note that Python 3 fixes this piece of weirdness -- attempting to compare a string to an int will cause a TypeError exception to occur.
Instead, you want to convert it into an int so you can do your size comparison properly.
x = int(raw_input())

How to call for multiple functions

I have program that checks wheter a social security numer is valid or not. Right now the program is One big function that step by step checks the number i input for several anomalies.
I want to make this big function into several different ones but i have no idea how and also no idea how to call all functions in a row couse they will rely alot on the previous functions results. im gonna try to show how is looks like:
#i want to make this into a separate function
def numberchecker(number):
if number > 15:
print('bad')
#i want to make this into a separate function
number1 = number.replace('-', '')
number2 = number1.replace(' ', '')
#i want to make this into a separate function
month= int(number1[2:][:2])
if month > 13:
print('very bad')
how shall i do this when some of the functions will rely on each others variables :/
Your first part is already a function, but confuses numbers and strings:
def check_num(num):
return num <= 15
your second part into a string-processing function:
def process_num(num):
return str(num).replace("-", "").replace(" ", "")
and the third into a boolean function
def correct_month(str_num):
return 1 <= str_num[2:4] <= 12
Now string them together:
if check_num(num):
str_num = process_num(num)
if not correct_month(str_num):
print('very bad')
else:
print('bad')
The crucial part of what you are trying to do is to return a value (in these cases, two booleans and a string) at the end of the function, so other functions can carry on and use it.
If all you do is validation, have all functions return either True or False, for example:
def numberchecker (number):
return number > 15
Then you can do:
all([numberchecker, current_month, ....])

Serial Key Generation and Validation

I'm toying around with writing creating a serial code generator/validator, but I can't seem to get how to do a proper check.
Here's my generator code:
# Serial generator
# Create sequences from which random.choice can choose
Sequence_A = 'ABCDEF'
Sequence_B = 'UVWQYZ'
Sequence_C = 'NOPQRS'
Sequence_D = 'MARTIN'
import random
# Generate a series of random numbers and Letters to later concatenate into a pass code
First = str(random.randint(1,5))
Second = str(random.choice(Sequence_A))
Third = str(random.randint(6,9))
Fourth = str(random.choice(Sequence_B))
Fifth = str(random.randint(0,2))
Sixth = str(random.choice(Sequence_C))
Seventh = str(random.randint(7,8))
Eighth = str(random.choice(Sequence_D))
Ninth = str(random.randint(3,5))
serial = First+Second+Third+Fourth+Fifth+Sixth+Seventh+Eighth+Ninth
print serial
I'd like to make a universal check so that my validation code will accept any key generated by this.
My intuition was to create checks like this:
serial_check = raw_input("Please enter your serial code: ")
# create a control object for while loop
control = True
# Break up user input into list that can be analyzed individually
serial_list = list(serial_check)
while control:
if serial_list[0] == range(1,5):
pass
elif serial_list[0] != range(1,5):
control = False
if serial_list[1] == random.choice('ABCDEF'):
pass
elif serial_list[1] != random.choice('ABCDEF'):
control = False
# and so on until the final, where, if valid, I would print that the key is valid.
if control == False:
print "Invalid Serial Code"
I'm well aware that the second type of check won't work at all, but it's a place holder because I've got no idea how to check that.
But I thought the method for checking numbers would work, but it doesn't either.
The expression `range(1, 5)' creates a list of numbers from 1 to 4. So in your first test, you're asking whether the first character in your serial number is equal to that list:
"1" == [1, 2, 3, 4]
Probably not...
What you probably want to know is whether a digit is in the range (i.e. from 1 to 5, I assume, not 1 to 4).
Your other hurdle is that the first character of the serial is a string, not an integer, so you would want to take the int() of the first character. But that will raise an exception if it's not a digit. So you must first test to make sure it's a digit:
if serial_list[0].isdigit() and int(serial_list[0]) in range(1, 6):
Don't worry, if it's not a digit, Python won't even try to evaluate the part after and. This is called short-circuiting.
However, I would not recommend doing it this way. Instead, simply check to make sure it is at least "1" and no more than "5", like this:
if "1" <= serial_list <= "5":
You can do the same thing with each of your tests, varying only what you're checking.
Also, you don't need to convert the serial number to a list. serial_check is a string and accessing strings by index is perfectly acceptable.
And finally, there's this pattern going on in your code:
if thing == other:
pass
elif thing != other:
(do something)
First, because the conditions you are testing are logical opposites, you don't need elif thing != other -- you can just say else, which means "whatever wasn't matched by any if condition."
if thing == other:
pass
else:
(do something)
But if you're just going to pass when the condition is met, why not just test the opposite condition to begin with? You clearly know how to write it 'cause you were putting it in the elif. Put it right in the if instead!
if thing != other:
(do something)
Yes, each of your if statements can easily be cut in half. In the example I gave you for checking the character range, probably the easiest way to do it is using not:
if not ("1" <= serial_list <= "5"):
Regarding your python, I'm guessing that when your wrote this:
if serial_list[0] == range(1,5):
You probably meant this:
if 1 <= serial_list[0] <= 5:
And when you wrote this:
if serial_list[1] == random.choice('ABCDEF'):
You probably meant this:
if serial_list[1] in 'ABCDEF':
There are various other problems with your code, but I'm sure you'll improve it as you learn python.
At a higher level, you seem to be trying to build something like a software activation code generator/validator. You should know that just generating a string of pseudo-random characters and later checking that each is in range is an extremely weak form of validation. If you want to prevent forgeries, I would suggest learning about HMAC (if you're validating on a secure server) or public key cryptography (if you're validating on a user's computer) and incorporating that into your design. There are libraries available for python that can handle either approach.

Categories

Resources