Python print day name of week from number - python

I'm creating a program which I have to put a number and when I run the program, the solution should be the day for that number. But I don't know how to do. The program I did was this:
num = 4
if(num = 1)
print('Monday')
elif(num = 2)
print('Tuesday')
elif(num = 3)
print('Wednesday')
elif(num = 4)
print('Thursday')
elif(num = 5)
print('Friday')
elif(num = 6)
print('Sunday')
elif(num = 7)
print('Saturday')
elif(num < 1)
print('Put a number between 1 and 7')
else(num > 7)
print('Put a number between 1 and 7')

In python, for statements like if, for, etc. You have to add : at the end of it.
And for comparing (for equal) you have to use == and not =
num = 4
if(num == 1):
print('Monday')
elif num == 2:
print('Tuesday')
.....
You can compare without parenthesis and it will work too.

num = 0
while num <= 7:
num += 1
if num == 1:
print('Monday')
elif num == 2:
print('Tuesday')
elif num == 3:
print('Wednesday')
elif num == 4:
print('Thursday')
elif num == 5:
print('Friday')
elif num == 6:
print('Saturday')
elif num == 7:
print('Sunday')

Related

how to write return function correct way without print return value?

def function(n):
if n % 2 != 0:
print('weird')
elif n % 2 == 0 and n in range(2, 5):
print('not weird')
elif n % 2 == 0 and n in range(6, 20):
print('weird')
elif n % 2 == 0 and n > 20:
print('not weird')
return n
while True:
n = int(input('enter the number: '))
print(function(n))
OUTPUT:
enter the number: 4
not weird
4
Above code, I don't want to print return number 4 again, how to write without representing the return number?
Change print(function(n)) to function(n)
def function(n):
if n % 2 != 0:
print('weird')
elif n % 2 == 0 and n in range(2, 5):
print('not weird')
elif n % 2 == 0 and n in range(6, 20):
print('weird')
elif n % 2 == 0 and n > 20:
print('not weird')
return n
while True:
n = int(input('enter the number: '))
function(n)
i mean u could just remove the print func at the end like so
def function(n):
if n % 2 != 0:
print('weird')
elif n % 2 == 0 and n in range(2, 5):
print('not weird')
elif n % 2 == 0 and n in range(6, 20):
print('weird')
elif n % 2 == 0 and n > 20:
print('not weird')
return n
while True:
n = int(input('enter the number: '))
function(n)
You can also just return from the function and not print it.
you can use it this way.. remove the return from the function statement and also remove the print when calling this function..
If you only remove the return but still call the function using print(func(n)) then the output would be something like
enter the number: 4
not weird
None
Maybe this is what you are looking for?
def function(n):
if n % 2 != 0:
print('weird')
elif n % 2 == 0 and n in range(2, 5):
print('not weird')
elif n % 2 == 0 and n in range(6, 20):
print('weird')
elif n % 2 == 0 and n > 20:
print('not weird')
while True:
n = int(input('enter the number: '))
function(n)

Python Code for Rock, Paper & Scissor Gave Wrong Output

Essence of this code is to depict Rock, Paper and Scissors game using Python language basically with for loop and if...else statements. I used PyScripter to run the code on Python 3.7.2 as engine. The def main() and if __name__ == '__main__' are PyScripter codes for running the machine
import random
def main():
pass
if __name__ == '__main__':
main()
tie_sum, comp_sum, human_sum = 0, 0, 0
name = input('Enter your firstname here: ')
for i in range(5):
tie_sum += tie_sum
comp_sum += comp_sum
human_sum += human_sum
comp_guess = random.randint(1, 3)
print(f'The computer guess option is {comp_guess}')
human_guess = int(input('Enter 1 as (rock), 2 as (paper) or 3 as (scissors):'))
if comp_guess == 1 and human_guess == 3:
comp_sum += 1
elif comp_guess == 1 and human_guess is 2:
human_sum += 1
elif comp_guess == 2 and human_guess == 3:
human_sum += 1
elif comp_guess == 3 and human_guess == 1:
human_sum += 1
elif comp_guess == 3 and human_guess == 2:
comp_sum += 1
elif comp_guess == 2 and human_guess == 1:
comp_sum += 1
else:
tie_sum += 1
print(f'The number of tie in this game is {tie_sum}')
if comp_sum > human_sum:
print('The winner of this game is the Computer.')
print(f'The comp_sum is {comp_sum}')
elif comp_sum < human_sum:
print(f'The winner of this game is {name}.')
print(f'The human sum is {human_sum}')
else:
print('This game ends in tie.')
print(f'The tie sum is {tie_sum}')
The reason for that is the first three lines in the for loop. You are increasing the sum of computer, human and tie while checking the condition and when you the loop iterates again, it sums up again. Here's the modified code:
import random
def main():
pass
if __name__ == '__main__':
main()
tie_sum, comp_sum, human_sum = 0, 0, 0
name = input('Enter your firstname here: ')
for i in range(5):
comp_guess = random.randint(1, 3)
human_guess = int(input('Enter 1 as (rock), 2 as (paper) or 3 as (scissors):'))
print(f'The computer guess option is {comp_guess}')
if comp_guess == 1 and human_guess == 3:
comp_sum += 1
elif comp_guess == 1 and human_guess == 2:
human_sum += 1
elif comp_guess == 2 and human_guess == 3:
human_sum += 1
elif comp_guess == 3 and human_guess == 1:
human_sum += 1
elif comp_guess == 3 and human_guess == 2:
comp_sum += 1
elif comp_guess == 2 and human_guess == 1:
comp_sum += 1
else:
tie_sum += 1
print(f'The number of tie in this game is {tie_sum}')
if comp_sum > human_sum:
print('The winner of this game is the Computer.')
print(f'The comp_sum is {comp_sum}')
elif comp_sum < human_sum:
print(f'The winner of this game is {name}.')
print(f'The human sum is {human_sum}')
else:
print('This game ends in tie.')
print(f'The tie sum is {tie_sum}')
Also, there was another modification, the computer guess is supposed to be printed after human's guess. I fixed that too. Hope that helps.

Passing multiple args into expanded if-else statements

I'm trying to pass multiple args. The first function creates the variables for age and gender. The second function redirects the arg based on gender. The 'num' variable should choose get_Male or get_Female and run through if-else statement until it finds equality. I'm new to python and it might be something simple. I just need to understand why the num variable doesn't pass. This is what I have so far...
A_Age = 0
B_Age = 4
C_Age = 8
D_Age = 15
E_Age = 25
F_Age = 38
G_Age = 48
H_Age = 60
def main():
#print('I am Rae...')
with open("output.txt", "r") as f:
gender = f.readline().split(":")[-1].strip()
age = f.readline().split(":")[-1].strip().split('-')[0]
print(gender) # Male
print(age) # 25-32
num = age
gen = gender
get_Gender(gen, num)
def get_Gender(gen, num):
#print('I am Rza...')
if gen == 'Male':
print('The gender is male.')
get_Male(gen, num)
else:
print('The gender is female.')
get_Male(gen, num)
return(gen, num)
def get_Male(gen, num):
#print('I am Gza...')
if gen == 'Male' and num == A_Age:
print('Unintentional Injuries')
elif num == B_Age:
print('Neoplasms')
elif num == C_Age:
print('Teenage injuries')
elif num == D_Age:
print('School fights')
elif num == E_Age:
print('High Blood Pressure')
elif num == F_Age:
print('Hypertension')
elif num == G_Age:
print("Heart Disease")
elif num == H_Age:
print('old age')
return
get_Female(gen, num)
return(gen, num)
def get_Female(gen, num):
#print('I am meth...')
if gen == 'Female' and num == A_Age:
print('Unintentional Injuries')
elif num == B_Age:
print('Neoplasms')
elif num == C_Age:
print('Teenage injuries')
elif num == D_Age:
print('School fights')
elif num == E_Age:
print('High Blood Pressure')
elif num == F_Age:
print('Hypertension')
elif num == G_Age:
print("Heart Disease")
elif num == H_Age:
print('old age')
return(gen, num)
main()
There are several ways you can make this code more straightforward and readable, which make it easier to understand how your code works.
First, you don't need nested if-else statements. You can use elif, which only gets evaluated if the previous if or elif evaluated to False. NB: A dictionary would simplify this function even further. I'll leave that up to you to discover.
Second, your functions get_Male and get_Female generally implement the same behavior, independent of the value of gen. Realizing this, we can consolidate these two functions into one and remove the gen parameter entirely.
Your functions get_Male and get_Female could look like this:
def print_symptom(num): # Previously `get_Male` and `get_Female`
if num == A_AGE:
print('Unintentional Injuries')
elif num == B_AGE:
print('Neoplasms')
elif num == C_AGE:
print('Teenage injuries')
elif num == D_AGE:
print('School fights')
elif num == E_AGE:
print('High Blood Pressure')
elif num == F_AGE:
print('Hypertension')
elif num == G_AGE:
print("Heart Disease")
elif num == H_AGE:
print('old age')
You don't need to return gen and num since you don't use them anywhere else. The same goes for your get_Gender function (see next code block).
Since we removed the gen parameter from print_symptoms, we need to check that gen is either 'Male' or 'Female' in the function that will call print_symptoms:
def get_gender(gen, num):
if gen == 'Male':
print('The gender is male.')
elif gen == 'Female': # Previously checked in `get_Female`
print('The gender is female.')
else:
print('Invalid gender. Exiting.')
return
print_symptom(gen, num)
Hopefully, these changes make it easier to understand what's going on in your code. Some other notes:
Check out this PEP which specifies naming conventions.
In general, functions should be defined before other functions which call them. That way, when you read a file from top to bottom, you know what a function does before you encounter it within another function.
A cleaned-up version of your code could look like this:
A_AGE = 0
B_AGE = 4
C_AGE = 8
D_AGE = 15
E_AGE = 25
F_AGE = 38
G_AGE = 48
H_AGE = 60
def print_symptom(num):
if num == A_AGE:
print('Unintentional Injuries')
elif num == B_AGE:
print('Neoplasms')
elif num == C_AGE:
print('Teenage injuries')
elif num == D_AGE:
print('School fights')
elif num == E_AGE:
print('High Blood Pressure')
elif num == F_AGE:
print('Hypertension')
elif num == G_AGE:
print("Heart Disease")
elif num == H_AGE:
print('old age')
def get_gender(gen, num):
if gen == 'Male':
print('The gender is male.')
elif gen == 'Female':
print('The gender is female.')
else:
print('Invalid gender. Exiting.')
return
print_symptom(num)
def main():
with open("output.txt", "r") as f:
gender = f.readline().split(":")[-1].strip()
age = f.readline().split(":")[-1].strip().split('-')[0]
print(gender) # Male
print(age) # 25-32
get_gender(gender, age)
main()
First, if I'm understanding your question correctly, the reason that your code isn't looking at both gender and age is because in get_Gender(), you check for gender but run get_Male() regardless of whether of what value you have for gen. The problem is specifically in this part:
def get_Gender(gen, num):
#print('I am Rza...')
if gen == 'Male':
print('The gender is male.')
get_Male(gen, num)
else:
print('The gender is female.')
get_Male(gen, num) #<---- I think this should be get_Female()
return(gen, num)
Another change that will make it easier to troubleshoot code like this: don't nest a bunch of if else statements like that. Instead, you can use "elif", which will only run if the prior if-statement resolved to False. Here is an example of how you can update your code:
def get_Male(gen, num):
if gen == 'Male' and num == A_Age:
print('Unintentional Injuries')
elif num == B_Age:
print('Neoplasms')
elif num == C_Age:
print('Teenage injuries')
elif num == D_Age:
print('School fights')
elif num == E_Age:
print('High Blood Pressure')
elif num == F_Age:
print('Hypertension')
elif num == G_Age:
print("Heart Disease")
elif num == H_Age:
print('old age')
return(gen, num)
Couple of things:
You can use the sentence elif instead of else and then if
You can use a dictionary to perform a kind of switch operation of other languages. I'd do something as follows:
switch = {
0: 'Unintentional Injuries',
4: 'Neoplasms',
8: 'Teenage injuries',
15: 'School fights',
25: 'High Blood Pressure',
38: 'Hypertension',
48: 'Heart Disease'.
60: 'old age'
}
disease = switch[num]
print(disease)

Getting wrong result when adding random values in string format

I'm trying to make a script that receives a number of desired random numbers as input, and then generates and prints them.
However, my script adds the numbers instead of joining the strings. I would like for the strings to join so it would generate the pins like:
Enter the amount of lunch pins to generate:
10
26141
128111
937502
2436
56516
83623
246317
My code:
import random
PTG = int(input("Enter the amount of pins to generate: \n"))
PG = 0
PS = ""
while PTG > PG:
RN1 = random.randint(0, 9)
RN2 = random.randint(0, 9)
RN3 = random.randint(0, 9)
RN4 = random.randint(0, 9)
RN5 = random.randint(0, 10)
RN6 = random.randint(0, 10)
if RN1 == 0:
PS += "0"
elif RN1 == 1:
PS += "1"
elif RN1 == 2:
PS += "2"
elif RN1 == 3:
PS += "3"
elif RN1 == 4:
PS += "4"
elif RN1 == 5:
PS += "5"
elif RN1 == 6:
PS += "6"
elif RN1 == 7:
PS += "7"
elif RN1 == 8:
PS += "8"
elif RN1 == 9:
PS += "9"
elif RN2 == 0:
PS += "0"
elif RN2 == 1:
PS += "1"
elif RN2 == 2:
PS += "2"
elif RN2 == 3:
PS += "3"
elif RN2 == 4:
PS += "4"
elif RN2 == 5:
PS += "5"
elif RN2 == 6:
PS += "6"
elif RN2 == 7:
PS += "7"
elif RN2 == 8:
PS += "8"
elif RN2 == 9:
PS += "9"
if RN3 == 0:
PS += "0"
elif RN3 == 1:
PS += "1"
elif RN3 == 2:
PS += "2"
elif RN3 == 3:
PS += "3"
elif RN3 == 4:
PS += "4"
elif RN3 == 5:
PS += "5"
elif RN3 == 6:
PS += "6"
elif RN3 == 7:
PS += "7"
elif RN3 == 8:
PS += "8"
elif RN3 == 9:
PS += "9"
elif RN4 == 0:
PS += "0"
elif RN4 == 1:
PS += "1"
elif RN4 == 2:
PS += "2"
elif RN4 == 3:
PS += "3"
elif RN4 == 4:
PS += "4"
elif RN4 == 5:
PS += "5"
elif RN4 == 6:
PS += "6"
elif RN4 == 7:
PS += "7"
elif RN4 == 8:
PS += "8"
elif RN4 == 9:
PS += "9"
elif RN5 == 0:
PS += "0"
elif RN5 == 1:
PS += "1"
elif RN5 == 2:
PS += "2"
elif RN5 == 3:
PS += "3"
elif RN5 == 4:
PS += "4"
elif RN5 == 5:
PS += "5"
elif RN5 == 6:
PS += "6"
elif RN5 == 7:
PS += "7"
elif RN5 == 8:
PS += "8"
elif RN5 == 9:
PS += "9"
elif RN5 == 10:
PS += ""
elif RN6 == 0:
PS += "0"
elif RN6 == 1:
PS += "1"
elif RN6 == 2:
PS += "2"
elif RN6 == 3:
PS += "3"
elif RN6 == 4:
PS += "4"
elif RN6 == 5:
PS += "5"
elif RN6 == 6:
PS += "6"
elif RN6 == 7:
PS += "7"
elif RN6 == 8:
PS += "8"
elif RN6 == 9:
PS += "9"
print(PS)
PG += 1
PS = ""
Python version: 3.7.4
import random
# PTG = int(input("Enter the amount of pins to generate: \n"))
PTG = 10
PG = 0
PS = ""
while PTG > PG:
RN1 = random.randint(0, 9)
RN2 = random.randint(0, 9)
RN3 = random.randint(0, 9)
RN4 = random.randint(0, 9)
RN5 = random.randint(0, 10)
RN6 = random.randint(0, 10)
PS = str(RN1) + str(RN2) + str(RN3) + str(RN4) + str(RN5) + str(RN6)
print(int(PS))
PG += 1
When I understand your code right, you want to generate n pins with 6 digits. You can do that a lot easier than you want to:
number_of_pins = int(input("Enter the amount of pins to generate: \n"))
pins = []
for i in range(number_of_pins):
pins.append(str(random.randint(100_000, 999_999)))
print(" ".join(pins))
Explaination:
pins = [] makes a new empty list to store the pins
for i in range(n): executes the following indented block n times.
pins.append(random.randint(100_000, 999_999)) generates a random number and adds it to the list. 100000 is the first number with 6 digits and 999999 is the last. (The _ is just for readability). str() converts it to a string.
print(" ".join(pins)) joins all the pins and puts a space between.
Let's go through your code step by step:
First, notice that random.randint returns an int. Therefore you need to convert it to a String.
You can use the str() function in order to convert it to a string, for example:
str(random.randint(0, 9))+"9"
will return a string like 59 (for example).
Therefore, when initializing each random number, you need to do it the following way, for example:
RN1 = str(random.randint(0, 9))
Then, instead of checking the value of each random variable, you can just add them up:
PS = RN1 + RN2 + RN3 + RN4 + RN5 + RN6
Furthermore, instead of using six different variables to handle the random values, you can use a for loop for the first four which are from 0 to 9:
for x in range(4):
RN = str(random.randint(0, 9))
PS += RN
And then add the remaining two that are between 0 and 10:
PS += str(random.randint(0, 10))
PS += str(random.randint(0, 10))

If comparison statements in Python

This block of code returns "cat", "dog", "hamster", and "unicorn", but it shouldn't return "unicorn" at all! Is there any reason for this?
if random.randint(0,10) < 5:
print("dog")
elif random.randint(0,10) > 5:
print("cat")
elif random.randint(0,10) == 5:
print("hamster")
else:
print("unicorn")
You're getting new random number on each comparison. What you probably meant is:
my_random_int = random.randint(0,10)
if my_random_int < 5:
print("dog")
elif my_random_int > 5:
print("cat")
elif my_random_int == 5:
print("hamster")
else:
print("unicorn")
random.randint is called again each time it is reached, potentially producing a different result each time (since that is the function's purpose).
If you want to repeatedly test with the same value, then store the value first.
You should create the random number only once!
val = random.randint(0,10)
if val < 5:
print("dog")
elif val > 5:
print("cat")
elif val == 5:
print("hamster")
else:
print("unicorn")
Assuming correct indentation, there's no reason for three random ints to be respectively >=5, <=5, and "not 5".
You probably meant to do this:
value = random.randint(0, 10)
if value < 5:
print("dog")
elif value > 5:
print("cat")
elif value == 5:
print("hamster")
else:
print("unicorn")
Now there are no chances of unicorns.
Your random number is different everytime you call random.randint so it might be 7 when you test the first if and go past it, then 3, then 4, and bam, you're in unicorn.
You should call random.randint only once at the beginning of your if, save its value and check it instead.
myrand = random.randint(0,10)
if myrand < 5:
print("dog")
elif myrand > 5:
print("cat")
elif myrand == 5:
print("hamster")
else:
print("unicorn")
The issue here is that you're generating a new random number each time. You should create it once and then assign it to a variable, then check that.
You're generating three different random numbers. What you're thinking is this:
random_number = random.randint(0,10)
if random_number < 5:
print("dog")
elif random_number > 5:
print("cat")
elif random_number == 5:
print("hamster")
else:
print("unicorn")
This code will only return one word, and will never return "unicorn".
You need to create only one random integer.
Your code should be:
myRandom = random.randint(0,10)
if myRandom < 5:
print("dog")
elif myRandom > 5:
print("cat")
elif myRandom == 5:
print("hamster")
else:
print("unicorn")

Categories