I have to make this calendar program and it isn't running - python

Here is my code that isn't running. It's a calendar program that counts days, days left in year, and leap years.

def leap_year():
if y % 400 == 0:
return 1
elif y % 100 == 0:
return 0
elif y % 4 == 0:
return 1
else:
return 0
def number_of_days(m, y):
y = leap_year(y)
if (m == 1 or m == 3 or m == 5 or m == 7 or m == 8 or m == 10 or m == 12):
return 31
elif (m == 4 or m == 6 or m == 9 or m == 11):
return 30
elif (m == 2):
if (y == 1):
return 29
elif (y == 0):
return 28
print(m)
def days_left(d, m, y):
daysleft = 0
for i in range(m,13):
days_left += number_of_days(m, y)
m += 1
print(days_left - d)
print("Please enter a date")
d = int(input("Day: "))
m = int(input("Month: "))
y = int(input("Year: "))
print("Menu: ")
print("1) Calculate the number of days in the given month: ")
print("2) Calculate the number of days left in the given year: ")
menu = int(input())
if menu == 1:
print(number_of_days(m, y))
elif menu == 2:
print(days_left(d, m, y))
else:
print("Invalid choice")

The first three errors in your code are:
function leap_year is declared with 0 parameters and is called with 1.
In the function days_left you have an assignment daysleft = 0 and then `days_left += 1
Wrong indentation in the same function # m += 1

Related

Python craps game: Wrong output

I'm a beginner and I can't figure out why I can't get the output I
want. It's a craps game. It's suppose to go like:
How many games do you want to play > 6 You rolled 5 + 2 = 7 You
win
What I got is something like: You rolled 1 + 6 = 7 You rolled 1 + 6 =
7 You rolled 1 + 6 = 7 You lose
import random
def rollDice():
roll_1 = random.randint(1,7)
roll_2 = random.randint(1,7)
return roll_1, roll_2
def determine_win_or_lose(dice1,dice2):
sum = dice1 + dice2
print("You rolled", dice1, "+", dice2, "=", sum )
if sum == '2' or '3' or '12':
return 0
elif sum == '7' or '11':
return 1
else:
print("Point is", sum)
determinePointValueResult(sum)
if determinePointValueResult(sum) == 1:
return 1
elif determinePointValueResult(sum) == 0:
return 0
def determinePointValueResult(sum):
point = sum
while sum != 7 and sum != point:
x, y = rollDice()
sum = x + y
if sum == point:
return 1
elif sum == '7':
return 0
print("You rolled", x, "+", y, "=", sum )
#==== MAIN =====
win = 0
lose = 0
game = int(input("How many games do you want to play > "))
for i in range(game):
x, y = rollDice()
determine_win_or_lose(x, y)
if determine_win_or_lose(x, y) == 1:
print("You win")
win = win + 1
elif determine_win_or_lose(x, y) == 0:
print("You lose")
lose = lose + 1
print("Game results: ", win, "wins and", lose, "losses")
Your issue come from the main, because you call the determine_win_or_lose function 3 times, the first one before the if (and i'm not sure why since you do nothing with it), a second time to check the condition of the if and a third time to check the condition of the elif.
Since it's this function that print the message, and you call the function 33 times each iteration of the for loop, it's normal to get the message printed 3 times.
(
Also since the determine_win_or_lose will always return 0 or 1 you don't really need an elif you can just do an if/else to achieve the same thing and simplify your code a bit.
)
So i'd suggest the following :
#==== MAIN =====
win = 0
lose = 0
game = int(input("How many games do you want to play > "))
for i in range(game):
x, y = rollDice()
result = determine_win_or_lose(x, y)
if result == 1:
print("You win")
win = win + 1
else:
print("You lose")
lose = lose + 1
print("Game results: ", win, "wins and", lose, "losses")
Obvious issues:
You call determine_win_or_lose too many times in your for loop. Change it to:
for i in range(game):
x, y = rollDice()
result = determine_win_or_lose(x, y)
if result == 1:
print("You win")
win = win + 1
elif result == 0:
print("You lose")
lose = lose + 1
Your check in determine_win_or_lose is incorrect. It should be something like:
def determine_win_or_lose(dice1,dice2):
sum = dice1 + dice2
print("You rolled", dice1, "+", dice2, "=", sum )
if sum == 2 or sum == 3 or sum == 12:
return 0
elif sum == 7 or sum == 11:
return 1
else:
print("Point is", sum)
determinePointValueResult(sum)
if determinePointValueResult(sum) == 1:
return 1
elif determinePointValueResult(sum) == 0:
return 0
In determinePointValueResult you shouldn't compare sum to a string, but an integer:
def determinePointValueResult(sum):
point = sum
while sum != 7 and sum != point:
x, y = rollDice()
sum = x + y
if sum == point:
return 1
elif sum == 7:
return 0
print("You rolled", x, "+", y, "=", sum )
It's possible that determine_win_or_lose and determinePointValueResult are returning None. You may need to change your elifs to elses or create a new else case.

Python Won't Run

My python program prints the first thing I wrote, and then loads forever and never prints the second task. What do I need to change with my code?
import random
def main():
money = 100
win = 0
loss = 0
draw = 0
bet = random.randint(5, 20)
print('You are starting with $100 and each round you will bet', bet, 'dollors')
while True:
x = random.randint(1, 6)
y = random.randint(1, 6)
z = x + y
if money == 0 or money == 200:
break
if z == 7 or z == 11:
money += bet
win += 1
elif z == 2 or z == 3 or z == 12:
loss += 1
money -= bet
else:
draw += 1
print('You ended up with', money, 'dollars, and you won', win, 'rounds, lost', \
loss, 'rounds, and drew', draw, 'rounds')
main()
Looks like it will never be able to satisfy all the conditions and is caught in a infinite loop
I think you have to use correct condition in the if condition and i suggest u to use a var to run the loop and to break the loop.
import random
def main():
money = 100
win = 0
loss = 0
draw = 0
bet = random.randint(5, 20)
print('You are starting with $100 and each round you will bet', bet, 'dollors')
loop = True
while loop:
x = random.randint(1, 6)
y = random.randint(1, 6)
z = x + y
if money <= 0 or money >= 200:
loop = False
if z == 7 or z == 11:
money += bet
win += 1
elif z == 2 or z == 3 or z == 12:
loss += 1
money -= bet
else:
draw += 1
print('You ended up with', money, 'dollars, and you won', win, 'rounds, lost', \
loss, 'rounds, and drew', draw, 'rounds')

for some reason i keep getting a unindent does not match any outer indentation level (i just started python)

if __name__ == '__main__':
n = int(input().strip())
if n % 2:
print('Weird')
elif for n in range(2,5) and % 2 == 0:
print(Not weird)
elif if n % 2 == 0 and for n in range(6,20):
print(weird)
elif if n % 2 == 0 and n > 20:
print('not weird')
if and elif in python should follow the same indentation level. Also, you are not supposed to use for in elif like that. Here's the corrected code-
if __name__ == '__main__':
n = int(input().strip())
if n % 2:
print('Weird')
elif n in range(2,5) and n % 2 == 0:
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')

How to add the value in string and go to next letter?

Two people participate in competition . There will be one easy, difficult and medium question
Difficulty Score
E 1
M 3
H 5
User will enter two strings e and b , and function should find out greater scores or tie.
My code is:
def winner(e, b):
sume = 0
sumb =0
highest = 0
x = False
for i in (range(len(erica))):
if (erica[i] =='E'):
sume +=1
x = True
elif (erica[i] =='M'):
sume = sume+3
x = True
elif (erica[i] =='H'):
sume +=5
x = True
return sume
if __name__ == '__main__':
erica = input()
bob = str(input())
print(winner(e,b))
When I enter HEM for e, it should give 9 but it only gives 5.
There is a problem with the return statement indentation. Try Below.
Also, you are not passing the correct variables.
def winner(e, b):
sume = 0
sumb =0
highest = 0
x = False
for i in (range(len(e))):
if (e[i] =='E'):
sume +=1
x = True
elif (e[i] =='M'):
sume = sume+3
x = True
elif (e[i] =='H'):
sume +=5
x = True
return sume
if __name__ == '__main__':
e = input()
b = str(input())
print(winner(e,b))
def winner(e, b):
sume = 0
sumb =0
highest = 0
for i in (range(len(e))):
if (e[i] =='E'):
sume +=1
elif (e[i] =='M'):
sume = sume+3
elif (e[i] =='H'):
sume +=5
for i in (range(len(b))):
if (b[i] =='E'):
sumb +=1
elif (b[i] =='M'):
sumb = sumb+3
elif (b[i] =='H'):
sumb +=5
if(sume>sumb):
return "Erica"
else:
return "Bob"
if __name__ == '__main__':
e = input()
b = str(input())
print(winner(e,b))

Python how to sum a list

I'm having trouble because I'm asking the user to input 6 numbers into a list and then total and average it depending on input from user. It's my HWK. Please help.
x = 0
list = []
while x < 6:
user = int(input("Enter a number"))
list.append(user)
x = x + 1
numb = input("Do you want a total or average of numbers?")
numb1 = numb.lower
if numb1 == "total":
Here is my answer:
def numberTest():
global x, y, z
L1 = []
x = 0
y = 6
z = 1
while(x < 6):
try:
user = int(input("Enter {0} more number(s)".format(y)))
print("Your entered the number {0}".format(user))
x += 1
y -= 1
L1.append(user)
except ValueError:
print("That isn't a number please try again.")
while(z > 0):
numb = input("Type \"total\" for the total and \"average\"").lower()
if(numb == "total"):
a = sum(L1)
print("Your total is {0}".format(a))
z = 0
elif(numb == "average"):
b = sum(L1)/ len(L1)
print("Your average is {0}".format(round(b)))
z = 0
else:
print("Please try typing either \"total\" or \"average\".")
numberTest()
I tried this a couple of times and I know it works. If you are confused about parts of the code, I will add comments and answer further questions.

Categories

Resources