I have a task to give each day of the week a number value. For example if the day is one the program will return monday. Everything i have right now works except for sunday aka day 7. I tried rearranging code but nothing works.
def dayOfWeek(day):
if day not in range(1, 7):
return "Unknown"
elif day == 1:
return "Monday"
elif day == 2:
return "Tuesday"
elif day == 3:
return "Wednesday"
elif day == 4:
return "Thursday"
elif day == 5:
return "Friday"
elif day == 6:
return "Saturday"
elif day == 7:
return "Sunday"
assert (dayOfWeek(0) == "Unknown")
assert (dayOfWeek(7) == "Sunday")
assert (dayOfWeek(1) == "Monday")
assert (dayOfWeek(3) == "Wednesday"
)
This is only offered as a future alternative. Functionally there is no difference.
You could also set a dictionary of the values
def dayOfWeek(day):
days = {
1: 'Monday',
2: 'Tuesday',
3: 'Wednesday',
4: 'Thursday',
5: 'Friday',
6: 'Saturday',
7: 'Sunday'
}
return days[day] if day in days else 'Unknown'
def dayOfWeek(day):
if day not in range(1, 8):
return "Unknown"
elif day == 1:
return "Monday"
elif day == 2:
return "Tuesday"
elif day == 3:
return "Wednesday"
elif day == 4:
return "Thursday"
elif day == 5:
return "Friday"
elif day == 6:
return "Saturday"
elif day == 7:
return "Sunday"
assert (dayOfWeek(0) == "Unknown")
assert (dayOfWeek(7) == "Sunday")
assert (dayOfWeek(1) == "Monday")
assert (dayOfWeek(3) == "Wednesday")
Use a range of 1 to 8 because python cuts off the last number.
In range function, python leaves the last number i.e if it is range(a,b) then the range goes from a to b-1. So your correct range will be (1,8) as said by Vedant Mehta. If you're confused with range, then use else statement. Your code:
def dayOfWeek(day):
if day == 1:
return "Monday"
elif day == 2:
return "Tuesday"
elif day == 3:
return "Wednesday"
elif day == 4:
return "Thursday"
elif day == 5:
return "Friday"
elif day == 6:
return "Saturday"
elif day == 7:
return "Sunday"
else:
return "Unknown"
assert (dayOfWeek(0) == "Unknown")
assert (dayOfWeek(7) == "Sunday")
assert (dayOfWeek(1) == "Monday")
assert (dayOfWeek(3) == "Wednesday")
Related
I'm new to python programming. This is my assignment for my first final sem. If the user enter their facility as "1" then user as "Yes". The system should show the price accordingly. This is my code but, when my input is "2" and "yes" or any number with "yes", it's always showing the output for facility == 4.I don't know what I'm doing wrong here. It's really hard to find the mistake in this huge codes. Is there anyways to shorten this. If yes, do help me out.
if facility == 1 and user == "Yes" or user == "YES" or user == "yes":
if day == "Monday" or day == "MONDAY" or day == "monday":
price = 100
elif day == "Tuesday" or day == "TUESDAY" or day == "tuesday":
price = 100
elif day == "Wednesday" or day == "WEDNESDAY" or day == "wednesday":
price = 100
elif day == "Thursday" or day == "THURSDAY" or day == "thursday":
price = 100
elif day == "Friday" or day == "FRIDAY" or day == "friday":
price = 100
elif day == " Saturday" or day == "SATURDAY" or day == "saturday":
price = 150
elif day == "Sunday" or day == "SUNDAY" or day == "sunday":
price = 150
if facility == 2 and user == "Yes" or user == "YES" or user == "yes":
if day == "Monday" or day == "MONDAY" or day == "monday":
price = 100
elif day == "Tuesday" or day == "TUESDAY" or day == "tuesday":
price = 100
elif day == "Wednesday" or day == "WEDNESDAY" or day == "wednesday":
price = 100
elif day == "Thursday" or day == "THURSDAY" or day == "thursday":
price = 100
elif day == "Friday" or day == "FRIDAY" or day == "friday":
price = 100
elif day == " Saturday" or day == "SATURDAY" or day == "saturday":
price = 150
elif day == "Sunday" or day == "SUNDAY" or day == "sunday":
price = 150
if facility == 3 and user == "Yes" or user == "YES" or user == "yes":
if day == "Monday" or day == "MONDAY" or day == "monday":
price = 200
elif day == "Tuesday" or day == "TUESDAY" or day == "tuesday":
price = 200
elif day == "Wednesday" or day == "WEDNESDAY" or day == "wednesday":
price = 200
elif day == "Thursday" or day == "THURSDAY" or day == "thursday":
price = 200
elif day == "Friday" or day == "FRIDAY" or day == "friday":
price = 200
elif day == " Saturday" or day == "SATURDAY" or day == "saturday":
price = 250
elif day == "Sunday" or day == "SUNDAY" or day == "sunday":
price = 250
if facility == 4 and user == "Yes" or user == "YES" or user == "yes":
if day == "Monday" or day == "MONDAY" or day == "monday":
price = 30
elif day == "Tuesday" or day == "TUESDAY" or day == "tuesday":
price = 30
elif day == "Wednesday" or day == "WEDNESDAY" or day == "wednesday":
price = 30
elif day == "Thursday" or day == "THURSDAY" or day == "thursday":
price = 30
elif day == "Friday" or day == "FRIDAY" or day == "friday":
price = 30
elif day == " Saturday" or day == "SATURDAY" or day == "saturday":
price = 60
elif day == "Sunday" or day == "SUNDAY" or day == "sunday":
price = 60
if facility == 1 and user == "No" or user == "NO" or user == "no":
if day == "Monday" or day == "MONDAY" or day == "monday":
price = 150
elif day == "Tuesday" or day == "TUESDAY" or day == "tuesday":
price = 150
elif day == "Wednesday" or day == "WEDNESDAY" or day == "wednesday":
price = 150
elif day == "Thursday" or day == "THURSDAY" or day == "thursday":
price = 150
elif day == "Friday" or day == "FRIDAY" or day == "friday":
price = 150
elif day == " Saturday" or day == "SATURDAY" or day == "saturday":
price = 200
elif day == "Sunday" or day == "SUNDAY" or day == "sunday":
price = 200
if facility == 2 and user == "No" or user == "NO" or user == "no":
if day == "Monday" or day == "MONDAY" or day == "monday":
price = 150
elif day == "Tuesday" or day == "TUESDAY" or day == "tuesday":
price = 150
elif day == "Wednesday" or day == "WEDNESDAY" or day == "wednesday":
price = 150
elif day == "Thursday" or day == "THURSDAY" or day == "thursday":
price = 150
elif day == "Friday" or day == "FRIDAY" or day == "friday":
price = 150
elif day == " Saturday" or day == "SATURDAY" or day == "saturday":
price = 200
elif day == "Sunday" or day == "SUNDAY" or day == "sunday":
price = 200
if facility == 3 and user == "No" or user == "NO" or user == "no":
if day == "Monday" or day == "MONDAY" or day == "monday":
price = 250
elif day == "Tuesday" or day == "TUESDAY" or day == "tuesday":
price = 250
elif day == "Wednesday" or day == "WEDNESDAY" or day == "wednesday":
price = 250
elif day == "Thursday" or day == "THURSDAY" or day == "thursday":
price = 250
elif day == "Friday" or day == "FRIDAY" or day == "friday":
price = 250
elif day == " Saturday" or day == "SATURDAY" or day == "saturday":
price = 300
elif day == "Sunday" or day == "SUNDAY" or day == "sunday":
price = 300
if facility == 4 and user == "No" or user == "no" or user == "NO":
if day == "Monday" or day == "MONDAY" or day == "monday":
price = 35
elif day == "Tuesday" or day == "TUESDAY" or day == "tuesday":
price = 35
elif day == "Wednesday" or day == "WEDNESDAY" or day == "wednesday":
price = 35
elif day == "Thursday" or day == "THURSDAY" or day == "thursday":
price = 35
elif day == "Friday" or day == "FRIDAY" or day == "friday":
price = 35
elif day == " Saturday" or day == "SATURDAY" or day == "saturday":
price = 65
elif day == "Sunday" or day == "SUNDAY" or day == "sunday":
price = 65
The problem is that and takes precedence over or. An expression like:
facility == 4 and user == "No" or user == "no" or user == "NO"
is the same as:
(facility == 4 and user == "No") or (user == "no") or (user == "NO"):
To have this expression do what you want it to do, you'd want to use something more like one of the following:
facility == 4 and (user == "No" or user == "no" or user == "NO")
facility == 4 and user in ("No", "no", "NO")
facility == 4 and user.lower() == "no"
Here is how I would write this logic more compactly -- use Enums to standardize all the string values (this protects you from typos and makes it easy to validate that the input is one of the expected values), and then put all the logic into a dict instead of a bunch of if/elif. The dict can be made more compact by the fact that you don't have individual prices for each day, simply different weekday/weekend prices:
from enum import Enum
class User(Enum):
YES = "yes"
NO = "no"
class Day(Enum):
MONDAY = "monday"
TUESDAY = "tuesday"
WEDNESDAY = "wednesday"
THURSDAY = "thursday"
FRIDAY = "friday"
SATURDAY = "saturday"
SUNDAY = "sunday"
def is_weekend(self) -> bool:
return self in (Day.SATURDAY, Day.SUNDAY)
# Prices are keyed on facility number(int), User, and Day.is_weekend().
prices = {
1: {User.YES: {False: 100, True: 150}, User.NO: {False: 150, True: 200}},
2: {User.YES: {False: 100, True: 150}, User.NO: {False: 150, True: 200}},
3: {User.YES: {False: 200, True: 250}, User.NO: {False: 250, True: 300}},
4: {User.YES: {False: 30, True: 60}, User.NO: {False: 35, True: 65}},
}
price = prices[facility][User(user.lower())][Day(day.lower()).is_weekend()]
The problem is that your if statement logic is not correct. When you do the following check:
if facility == 4 and user == "Yes" or user == "YES" or user == "yes":
This is functionally equivalent to:
if (facility == 4 and user == "Yes") or (user == "YES") or (user == "yes"):
Meaning, if the user chose a variation of "Yes", then the if statement will always be entered. Your logic should look like this instead:
if facility == 4 and (user == "Yes" or user == "YES" or user == "yes"):
Which could be simplified to this, by just comparing the lowercase version of the user string:
if facility == 4 and user.lower() == "yes":
You could simplify all your logic to use lowercase, which would simplify your if statements to look like this:
if facility == 4 and user.lower() == "yes":
if day.lower() == "monday":
price = 30
elif day.lower() == "tuesday":
price = 30
elif day.lower() == "wednesday":
price = 30
elif day.lower() == "thursday":
price = 30
elif day.lower() == "friday":
price = 30
elif day.lower() == "saturday":
price = 60
elif day.lower() == "sunday":
price = 60
Try using if/elif instead of if/if.
Also, put brackets around the if logic, like so:
if facility == 4 and (user == "Yes" or user == "YES" or user == "yes":)
To shorten this code you can use .lower() which turns a String into lowercase.
user = user.lower()
day = day.lower()
This will allow you to just check whether day == "monday" instead of looking through different capitalizations of Monday.
You could also do
if day in ["monday","tuesday","wednesday","thursday","friday","saturday","sunday"]
This will remove most of your elif statements and just put them all in one.
This question already has answers here:
Remap values in pandas column with a dict, preserve NaNs
(11 answers)
Closed 4 years ago.
It's a very basic question but somehow my loop does not work. I have a pandas dataframe with two columns; path and word. A neural network predicted the outcome value in word, however this is still an integer. I wrote a for loop to replace those integers with words but there are no changes in the df. My df:
path word
0 f7f172c01bec6a3e9a36dcafdf9e02e7df3522e4.wav 21
1 c17c29e392c57a9243ed52175568b40c04912194.wav 21
2 eea9239d4986b1f7dbffdcce76e0fce6e5f38ca8.wav 21
3 4fec4b033ba19d1ac875c0c062fda2869dbece73.wav 21
my loop:
for i in df['word']:
if i == 0:
i == "backward"
elif i == 1:
i == "bed"
elif i == 2:
i == "bird"
elif i == 3:
i == "cat"
elif i == 4:
i == "dog"
elif i == 5:
i == "down"
elif i == 6:
i == "eight"
elif i == 7:
i == "five"
elif i == 8:
i == "follow"
elif i == 9:
i == "forward"
elif i == 10:
i == "four"
elif i == 11:
i == "go"
elif i == 12:
i == "happy"
elif i == 13:
i == "house"
elif i == 14:
i == "learn"
elif i == 15:
i == "left"
elif i == 16:
i == "marvin"
elif i == 17:
i == "nine"
elif i == 18:
i == "no"
elif i == 19:
i == "off"
elif i == 20:
i == "on"
elif i == 21:
i == "one"
elif i == 22:
i == "right"
elif i == 23:
i == "seven"
elif i == 24:
i == "sheilla"
elif i == 25:
i == "six"
elif i == 26:
i == "stop"
elif i == 27:
i == "three"
elif i == 28:
i == "tree"
elif i == 29:
i == "two"
elif i == 30:
i == "up"
elif i == 31:
i == "visual"
elif i == 32:
i == "wow"
elif i == 33:
i == "yes"
elif i == 34:
i == "zero"
You can use pd.Series.map:
#Add the rest of your mapping here (I just included a few)
mapping = {0: 'backward', 1: 'bed', 21: 'one', 22: 'right'}
df['word'] = df['word'].map(mapping)
Returns:
path word
0 f7f172c01bec6a3e9a36dcafdf9e02e7df3522e4.wav one
1 c17c29e392c57a9243ed52175568b40c04912194.wav one
2 eea9239d4986b1f7dbffdcce76e0fce6e5f38ca8.wav one
3 4fec4b033ba19d1ac875c0c062fda2869dbece73.wav one
everytime I run my code I get attribute error and int has no randint, but when I looked online how to do random, that is what it told me to do, please help.
def gorandom():
if random.randint(1,8) == 1:
turtle.goto(-250,250)
elif random.randint(1,8) == 2:
turtle.goto(0,250)
elif random.randint(1,8) == 3:
turtle.goto(250,250)
elif random.randint(1,8) == 4:
turtle.goto(250,0)
elif random.randint(1,8) == 5:
turtle.goto(250,-250)
elif random.randint(1,8) == 6:
turtle.goto(0,-250)
elif random.randint(1,8) == 7:
turtle.goto(-250,-250)
else:
turtle.goto(-250,0)
You are missing an import. Please add
import random
To the top of your file.
try this:
import random
def gorandom():
if random.randint(1,8) == 1:
turtle.goto(-250,250)
elif random.randint(1,8) == 2:
turtle.goto(0,250)
elif random.randint(1,8) == 3:
turtle.goto(250,250)
elif random.randint(1,8) == 4:
turtle.goto(250,0)
elif random.randint(1,8) == 5:
turtle.goto(250,-250)
elif random.randint(1,8) == 6:
turtle.goto(0,-250)
elif random.randint(1,8) == 7:
turtle.goto(-250,-250)
else:
turtle.goto(-250,0)
And be sure your goto(x,y) function works ;)
found solution, thanks guys for help
from random import randint
def gorandom():
if randint(1,8) == 1:
turtle.goto(-250,250)
elif randint(1,8) == 2:
turtle.goto(0,250)
elif randint(1,8) == 3:
turtle.goto(250,250)
elif randint(1,8) == 4:
turtle.goto(250,0)
elif randint(1,8) == 5:
turtle.goto(250,-250)
elif randint(1,8) == 6:
turtle.goto(0,-250)
elif randint(1,8) == 7:
turtle.goto(-250,-250)
else:
turtle.goto(-250,0)
Basically I need help creating a function to read a given parameter which is a list, go through each digit of the list, checking them and adding their binary value to a different list. I am trying this code, but it's not working the way I think it should. Any help is welcome: The side parameter is there to help sort the binary. I have two sets and depending on which 'side' the digits in the list are on, they have a different binary code.
def bin_convert(upc, side):
bin_list = []
if side == 0:
for digit in upc:
if digit == 0:
bin_list.append(0001101)
elif digit == 1:
bin_list.append(0011001)
elif digit == 2:
bin_list.append(0010011)
elif digit == 3:
bin_list.append(0111101)
elif digit == 4:
bin_list.append(0100011)
elif digit == 5:
bin_list.append(0110001)
elif digit == 6:
bin_list.append(0101111)
elif digit == 7:
bin_list.append(0111011)
elif digit == 8:
bin_list.append(0110111)
elif digit == 9:
bin_list.append(0001011)
print bin_list
return bin_list
else:
for digit in upc:
if digit == 0:
bin_list.append(1110010)
elif digit == 1:
bin_list.append(1100110)
elif digit == 2:
bin_list.append(1101100)
elif digit == 3:
bin_list.append(1000010)
elif digit == 4:
bin_list.append(1011100)
elif digit == 5:
bin_list.append(1001110)
elif digit == 6:
bin_list.append(1010000)
elif digit == 7:
bin_list.append(1000100)
elif digit == 8:
bin_list.append(1001000)
elif digit == 9:
bin_list.append(1110100)
print bin_list
return bin_list
I have code here for moving a card from the deck to the foundation pile. I've imported the necessary details, etc. My problem is, it's too long. Is there any way to make it shorter? How? Thanks :)
def dtof():
suit = raw_input("enter suit: ")
v = trash.pop()
if suit == "D":
if card.suitNumber[v.suit] == 1:
if card.rankNumber[v.rank] == 0:
Diamond.append(v)
elif card.rankNumber[v.rank] == card.rankNumber[Diamond[-1].rank] + 1:
Diamond.append(v)
else:
trash.append(v)
return Diamond[-1]
else:
trash.append(v)
elif suit == "H":
if card.suitNumber[v.suit] == 2:
if card.rankNumber[v.rank] == 0:
Heart.append(v)
elif card.rankNumber[v.rank] == card.rankNumber[Heart[-1].rank] + 1:
Heart.append(v)
else:
trash.append(v)
return Heart[-1]
else:
trash.append(v)
elif suit == "C":
if card.suitNumber[v.suit] == 4:
if card.rankNumber[v.rank] == 0:
Clubs.append(v)
elif card.rankNumber[v.rank] == card.rankNumber[Clubs[-1].rank] + 1:
Clubs.append(v)
else:
trash.append(v)
return Clubs[-1]
else:
trash.append(v)
elif suit == "S":
if card.suitNumber[v.suit] == 3:
if card.rankNumber[v.rank] == 0:
Spade.append(v)
elif card.rankNumber[v.rank] == card.rankNumber[Spade[-1].rank] + 1:
Spade.append(v)
else:
trash.append(v)
return Spade[-1]
else:
trash.append(v)
else:
trash.append(v)
Consider merging Diamond, Heart, Clubs and Spade into a single dictionary, with the key being suit.