Converting string to float unsuccessful - python

def main():
x = abc_to_def(input("What time is it? "))
if x >= "7.0" and x <= "8.0":
print("breakfast time")
elif x >= "12.0" and x <= "13.0":
print("lunch time")
elif x >= "18.0" and x <= "19.0":
print("dinner time")
else:
print()
def abc_to_def(p):
hours, minutes = p.split(":")
if float(int(hours)) + int(minutes)/int(60):
return p
if __name__ == "__main__":
main()
When I run the code in the terminal, everything is correct, but when I run check50 it says my output is "".

#!/usr/local/bin/python3
def main():
y = input("What time is it? ")
x = abc_to_def(y)
if x >= 7.0 and x <= 8.0:
print("breakfast time")
elif x >= 12.0 and x <= 13.0:
print("lunch time")
elif x >= 18.0 and x <= 19.0:
print("dinner time")
else:
print("no food")
def abc_to_def(p):
hours, minutes = p.split(":")
p = float(int(hours)) + int(minutes)/int(60)
return p
if __name__ == "__main__":
main()

Related

python relational inclusive operators not inludind start and end values

The goal of the function is to output a value in a given range. Including the start and end value if it is entered as input. The function only outputs the expected result for values at the start and between the range.
def main():
#assume user input will be formatted in 24-hour time as #:## or ##:##
time = input("What is the time: ")
if time >= "7.0" and time <= "8.0":
print("breakfast time")
elif time >= "12.0" and time <= "13.0":
print("lunch time")
elif time >= "18.0" and time <= "19.0":
print("dinner time")
def convert(time):
h, m = time.split(":")
time = float(((float(h) * (60)) + float(m)) / 60)
return time
if __name__ == "__main__":
main()
current output when input is i.e 8:00 --> ""
expected output when input is i.e 8:00 --> breakfast
You need first to call you convert method, and change the conditions to avoid exception comparing string with float
def main():
time = convert(input("What is the time: "))
if time >= 7.0 and time <= 8.0:
print("breakfast time")
elif time >= 12.0 and time <= 13.0:
print("lunch time")
elif time >= 18.0 and time <= 19.0:
print("dinner time")
def convert(time):
h, m = time.split(":")
time = float((int(h) * (60) + int(m)) / 60)
return time
if __name__ == "__main__":
main()
Use datetime.strptime to parse text representations of times, then compare them to other time values.
from datetime import datetime, time
def main():
#assume user input will be formatted in 24-hour time as #:## or ##:##
current_time = input("What is the time: ")
current_time = datettime.strptime(current_time, "%H:%M").time()
if time(7) <= now <= time(8):
print("breakfast time")
elif time(12) <= now <= time(13):
print("lunch time")
elif time(18) <= now <= time(19):
print("dinner time")
if __name__ == "__main__":
main()

making recursion faster using array

I was trying to get this recursion faster but when I use numbers 50 and 44.4 it takes too long my desired outcome for those numbers is -800555.6302016332
z = int(input())
x = float(input())
def rec(n):
global x
l = {}
if n == 0:
return -1
elif n == 1:
return x
elif n == 2:
return -(x+1)/3
else:
if n in l:
return l[n]
value = float((n/x)*rec(n-1) + ((-1)**n)*((n+1)/(n-1)) * rec(n-2) + ((n-1)/(2*x))*rec(n-3))
l[n] = value
return value
print(rec(z))
You are reinitializing your dictionary l = {} each time you recurse. Making l a global var should fix your problem:
l = {}
def rec(n):
global x
global l
if n == 0:
return -1
elif n == 1:
return x
elif n == 2:
return -(x+1)/3
else:
if n in l:
return l[n]
value = float((n/x)*rec(n-1) + ((-1)**n)*((n+1)/(n-1)) * rec(n-2) + ((n-1)/(2*x))*rec(n-3))
l[n] = value
return value
You could also use functools.lru_cache which does memoization for you:
import functools
#functools.lru_cache
def rec(n):
global x
if n == 0:
return -1
elif n == 1:
return x
elif n == 2:
return -(x+1)/3
else:
return float((n/x)*rec(n-1) + ((-1)**n)*((n+1)/(n-1)) * rec(n-2) + ((n-1)/(2*x))*rec(n-3))
I would also suggest avoiding the use of global variables:
import functools
def rec(n, x):
#functools.lru_cache
def recurse(n):
if n == 0:
return -1
elif n == 1:
return x
elif n == 2:
return -(x+1)/3
else:
return float((n/x)*recurse(n-1) + ((-1)**n)*((n+1)/(n-1)) * recurse(n-2) + ((n-1)/(2*x))*recurse(n-3))
return recurse(n)
def main():
n = int(input())
x = float(input())
print(rec(n, x))
if __name__ == "__main__":
main()

Python Simple American Football Simulator Not Running

Simple football simulation for my class project. I simplified a lot of the rules and aspects of the game, so everything isn't very accurate compared to an actual football game. However, When I try running my function, only the line of code which prints the name of the QB throwing to the receiver prints. For example when I run it only, "Jared Goff throws to Brandin Cooks for 33 yards!" shows up in the display. How can I get the whole function to run/print? Not sure where I went wrong.
import random
rams_qb = ["Jared Goff"]
patriots_qb = ["Tom Brady"]
rams_receivers = ["Cooper Kupp", "Brandin Cooks"]
patriots_receivers = ["Julian Edelman", "Josh Gordon"]
rams_score = 0
patriots_score = 0
quarter_time = 900
def remaining_time():
global quarter_time
global rams_score
global patriots_score
if quarter_time > 0:
if random.randint(0,100) < 50:
return rams_possesion()
else:
return patriots_possesion()
elif quarter_time == 0:
if rams_score > patriots_score:
print ("Rams Win!")
else:
print ("Patriots Win!")
def rams_possesion():
global quarter_time
global rams_score
rams_ball_position = 50
rams_downs = 1
if rams_ball_position == rams_ball_position + 10:
rams_downs = 1
else:
rams_downs += 1
if rams_ball_position == 100:
rams_score == rams_score + 6
print ("RAMS TOUCHDOWN!")
return rams_fieldgoal
if rams_downs <= 4:
rams_yardage_gained = random.randint(0,50)
print ((random.choice(rams_qb)),("throws to"),
(random.choice(rams_receivers)),("for"),(str(rams_yardage_gained)),
("yards!"))
rams_ball_position == rams_ball_position + rams_yardage_gained
quarter_time -= random.randint(0,30)
if rams_downs >= 5:
return patriots_possesion
def rams_fieldgoal():
global rams_score
if random.randint(0,100) < 83:
rams_score == rams_score + 1
print ("RAMS SCORE FIELDGOAL!")
else:
print ("RAMS MISS FIELDGOAL")
return patriots_possesion
def patriots_possesion():
global patriots_score
patriots_ball_position = 50
patriots_downs = 1
if patriots_ball_position == patriots_ball_position + 10:
patriots_downs = 1
else:
patriots_downs += 1
if patriots_ball_position == 100:
patriots_score == patriots_score + 6
print ("PATRIOTS TOUCHDOWN!")
return patriots_fieldgoal
if patriots_downs <= 4:
patriots_yardage_gained = random.randint(0,50)
print ((random.choice(patriots_qb)),("throws to"),
(random.choice(patriots_receivers)),("for"),(str(patriots_yardage_gained)),
("yards!"))
patriots_ball_position == patriots_ball_position +
patriots_yardage_gained
if patriots_downs >= 5:
return rams_possesion()
def patriots_fieldgoal():
global patriots_score
if random.randint(0,100) < 87:
patriots_score == patriots_score + 1
print ("PATRIOTS SCORE FIELDGOAL!")
else:
print ("PATRIOTS MISS FIELDGOAL")
return rams_possesion()
remaining_time()
My best guess here is that your comparisons are not taking into account the fact that the values can go over the target. For example, when checking yardage, you're checking for equality to 100, but you aren't guaranteeing that the value won't go over 100. You have a similar bug with checking if the quarter_time == 0 when it should be <= 0 to make sure it triggers.
I can't tell you if changing these comparisons will fix your program, but try these changes and comment how the behavior changes.

Begginer- Doesn't let me input 'x'

Doesn't let me to input values to x.
It runs and it says that is completed with exit code 0. Why?
def donuts():
x = int(input("How many donutes?"))
if x < 10:
print("Numbers of donuts: "+ x)
else:
print("Number of donute: many")
def litere():
x = input("Type a word! : ")
if len(x) <= 2:
print("NULL")
else:
k = len(x) -1
print(x[0:3] + x[k-2])
if ' name ' == ' main ':
donuts()
__name__ is a variable. It should have quotes around it. When you surround it with quotes python treats it like a regular string. Replace the bottom with
if __name__ == "__main__":
donuts()
Remove quotes around '__name__' so it becomes __name__:
def donuts():
x = int(input("How many donutes?"))
if x < 10:
print("Numbers of donuts: "+ x)
else:
print("Number of donute: many")
def litere():
x = input("Type a word! : ")
if len(x) <= 2:
print("NULL")
else:
k = len(x) -1
print(x[0:3] + x[k-2])
if __name__ == "__main__":
donuts()
Here is the change I made to your code:
if __name__ == "__main__":
donuts()

python: I have this code (below) but it prints in reverse, how can I reverse it

How can I reverse the output?
def dectohex(num):
z = int(0)
y = int(0)
if num > 0:
z = num // 16
y = int((num % 16))
if y == 10:
print("A", end = "")
elif y == 11:
print("B", end = "")
elif y == 12:
print("C", end = "")
elif y == 13:
print("D", end = "")
elif y == 14:
print("E", end = "")
elif y == 15:
print("F", end = "")
elif y == 0 or 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9:
print(y, end = "")
dectohex(z)
inp = int(input("Enter number "))
dectohex(inp)
Calling the recursive function earlier will reverse the output:
def dectohex(num):
if num > 0:
z = num // 16
dectohex(z)
y = num % 16
if y == 10:
print("A", end = "")
elif y == 11:
print("B", end = "")
elif y == 12:
print("C", end = "")
elif y == 13:
print("D", end = "")
elif y == 14:
print("E", end = "")
elif y == 15:
print("F", end = "")
else:
print(y, end = "")
Note that I also made some other optimization to the function. Notably, I removed unnecessary initialization, casting and simplified the if-chain, since the number y is known to be an integer with 0 <= y <= 15.

Categories