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

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))

Related

Stop running function when one variable gets to zero

Hope you can help me. Please see the code below. What I am trying to achieve is for code to stop when "x" or "y" gets to 0. At the moment, "x" gets to 0 first, but function_b() is still run after that. I would like that if "x" gets to 0, function_b() would not run afterwards. How do I do that?
Thank you
from random import randint
x = 10
y = 10
def roll_dice():
random_dice = randint(1, 6)
return random_dice
def function_a():
global x
while x > 0:
dice = roll_dice()
if dice >= 1:
x -= 5
else:
print('Better luck next time')
if x <= 0:
break
else:
function_b()
def function_b():
global y
while y > 0:
dice = roll_dice()
if dice >= 1:
y -= 5
else:
print('Better luck next time')
if y <= 0:
break
else:
function_a()
def turns():
if x > 0:
function_a()
else:
print('good game')
if y > 0:
function_b()
else:
print('good game')
turns()
I made some adjustement on your code :
from random import randint
x = 10
y = 10
def roll_dice():
random_dice = randint(1, 6)
return random_dice
def function_a():
global x
while x > 0 and y != 0:
dice = roll_dice()
if dice >= 1:
x -= 5
else:
print('Better luck next time')
if x <= 0:
break
else:
function_b()
def function_b():
global y
while y > 0 and x !=0:
dice = roll_dice()
if dice >= 1:
y -= 5
else:
print('Better luck next time')
if y <= 0:
break
else:
function_a()
def turns():
if x > 0:
function_a()
else:
print('good game')
if y > 0:
function_b()
else:
print('good game')
turns()

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

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

why label is not being configured in function?

my score label or chances count is not configured when i run the function. can anyone help on this? the question label changes perfectly but the score label does not change and i am unable to understand why is that so.
def checkans(self):
chances = 0
score = 0
count = 0
while chances == 3:
break
else:
operations=['+', '-', '*']
a = random.randint(1,15)
b = random.randint(1,15)
random.shuffle(operations)
op = operations[0]
self.label.configure(text=(a,op,b))
if op == '+':
ans = a + b
if op == '-':
ans = a - b
if op == '*':
ans = a * b
self.e.focus_set()
if self.e.get() == ans:
score += 1
self.scorelabel.configure(text=('score', score))
else:
chances += 1
Try this:
def checkans(self):
chances = 0
score = 0
count = 0
while chances < 3:
operations=['+', '-', '*']
a = random.randint(1,15)
b = random.randint(1,15)
random.shuffle(operations)
op = operations[0]
self.label.configure(text=(a,op,b))
if op == '+':
ans = a + b
if op == '-':
ans = a - b
if op == '*':
ans = a * b
self.e.focus_set()
if self.e.get() == ans:
score += 1
self.scorelabel.configure(text=('score', score))
else:
chances += 1
When you use while with else the else only executes once.

Rock, Paper, Scissor, Spock, Lizard in python, Player 2 automatically wins

for an exercise we need to recreate the game played by the members of the bigbang theory: Rock, Paper, Scissor, Spock, Lizard. I managed to recreate it almost completely, the only problem is: Player 2 automatically wins. Can someone tell me where I need to change the code and also explain why?
import sys
t = len(sys.argv)
if(t < 2 or t > 3):
print("Usage: rpsls.py symbool1 symbool2")
exit()
i = 1
while (i > 0):
a = sys.argv[1]
b = sys.argv[2]
a = a.lower()
b = b.lower()
if(a != "rock" and a != "paper" and a != "scissor" and a != "lizard" and a != "spock"):
print("What's that? please use a real symbol!")
elif(b != "rock" and b != "paper" and b != "scissor" and b != "lizard" and b != "spock"):
print("What's that? please use a real symbol!")
else:
if (a == "paper" and b == "scissor"):
s = True
i = 0
else:
s = False
i = 0
if(a == "paper" and b == "rock"):
s = True
i = 0
else:
s = False
i = 0
if(a == "rock" and b == "lizard"):
s = True
i = 0
else:
s = False
i = 0
if(a == "lizard" and b == "spock"):
s = True
i = 0
else:
s = False
i = 0
if(a == "spock" and b == "scissors"):
s = True
i = 0
else:
s = False
i = 0
if(a == "scissor" and b == "lizard"):
s = True
i = 0
else:
s = False
i = 0
if(a == "lizard" and b == "paper"):
s = True
i = 0
else:
s = False
i = 0
if(a == "paper" and b == "spock"):
s = True
i = 0
else:
s = False
i = 0
if(a == "spock" and b == "rock"):
s = True
i = 0
else:
s = False
i = 0
if(a == "rock" and b == "scissor"):
s = True
i = 0
else:
s = False
i = 0
if(a == b):
print("It's a tie!")
i = 0
exit()
if(s == True):
print("Player 1 wins!")
if(s == False):
print("Player 2 wins!")
Each of your if statements has an else. Only one of the if statements can be true, so that means that all the other else statements are evaluated. The result of that is that the last else statement - which sets s to False - will "win", so player 2 wins.
You should drop all your else statements, and restructure your code as a series of if...elif... blocks:
if a == "paper" and b == "scissor":
s = True
i = 0
elif a == "paper" and b == "rock":
(Note, if conditions don't need parentheses.)

Changing string into integers for variables inside function, Python

why the assign() function does not change the string into integer?
the assign function does not return the values i set in if statement!
p1 = raw_input("Player 1 ?")
p2 = raw_input("Player 2 ?")
def assign(n):
if n == "r":
return (1)
elif n == "s":
n = 2
elif n == "p":
n = 3
else:
print "Wrong input!"
return n
assign(p1)
assign(p2)
print p1
print p2
if p1 - p2 == 0:
print "Tie!"
elif (p1 - p2) / 2 != 0:
print " Player 1 is winner!"
else:
print" Player 2 is the winner!"
The variable n inside the function is distinct from the expression (or variable) supplied as an argument. As such, assigning to n inside the function will not affect any of the variables outside. (This is because Python is not Call By Reference.)
Instead, use the return value, eg.
def classifyInput(n):
if n == "r":
return 1
elif n == "s":
return 2
elif n == "p":
return 3
else:
print "Wrong input!"
# implicit: return None
p1_inp = raw_input("Player 1 ?")
p1 = classifyInput(p1_inp)

Categories

Resources