Create a function determine_grade - function

how can I create a function that determines_grade which accepts the grade between 0-100 and returns the letter grade based on the following scale?
. 90-100 A
. 80-89 B
. 70-79 C
. 60-69 D
. below 60 F
and call my function with each of the following argument
function call score
1 90
2 85
3 73
4 62
5 58

In Python, you can use the if/elif/else logic in your function, and since this logic evaluates from the "top-down", after checking if the input is valid, only a single criteria has to be checked at each step. For example:
def determine_grade(score):
if score < 0 or score > 100:
return "Score not between 0-100"
elif score >= 90:
return "A"
elif score >= 80:
return "B"
elif score >= 70:
return "C"
elif score >= 60:
return "D"
else:
return "F"

Related

How to Convert base 10 to base 2 and find the base 10 integer that denotes most consecutive 1s?

I have been trying to convert base 10 to base 2 and finding the base 10 integer that denotes most consecutive 1s
if __name__ == '__main__':
n = int(input().strip())``
outcomes = 0
biggest = 0
while n > 0:
if n % 2 == 1:
outcomes += 1
if outcomes > biggest:
biggest = outcomes
else:
result = 0
print(biggest)
those are my codes please correct where i'm wrong

How many binary numbers with N bits if no more than M zeros/ones in a row

Is there an equation I can use for arbitrary M and N?
Example, N=3 and M=2:
3 bits allow for 8 different combinations, but only 2 of them do not contain more than 2 same symbols in a row
000 - Fails
001 - Fails
010 - OK
011 - Fails
100 - Fails
101 - OK
110 - Fails
111 - Fails
One way to frame the problem is as follows: we would like to count binary words of length n without runs of length m or larger. Let g(n, m) denote the number of such words. In the example, n = 3 and m = 2.
If n < m, every binary word works, and we get g(n, m) = 2^n words in total.
When n >= m, we can choose to start with 1, 2, ... m-1 repeated values,
followed by g(n-1, m), g(n-2, m), ... g(n-m+1, m) choices respectively. Combined, we get the following recursion (in Python):
from functools import lru_cache
#lru_cache(None) # memoization
def g(n, m):
if n < m:
return 2 ** n
else:
return sum(g(n-j, m) for j in range(1, m))
To test for correctness, we can compute the number of such binary sequences directly:
from itertools import product, groupby
def brute_force(n, k):
# generate all binary sequences of length n
products = product([0,1], repeat=n)
count = 0
for prod in products:
has_run = False
# group consecutive digits
for _, gp in groupby(prod):
gp_size = sum(1 for _ in gp)
if gp_size >= k:
# there are k or more consecutive digits in a row
has_run = True
break
if not has_run:
count += 1
return count
assert 2 == g(3, 2) == brute_force(3, 2)
assert 927936 == g(20, 7) == brute_force(20, 7)

writing function , giving input and printing them with a condition

I want to write a function, which will accept parameter and it will print with a condition (It's output will depend on the input). My program is giving key error. I am looking for an output like:
This number is less than 0 and it's spelling is one hundred
thirteen
and my code is:
def word(num):
d1= {0:'Zero',1:'One',2:'Two',3:'Three',4:'Four',5:'Five',6:'Six',7:'Seven',8:'Eight',9:'Nine',10:'Ten',11:'Eleven',12:'Twelve',13:'Thirteen',14:'Fourteen',15:'Fifteen',16:'Sixteen',17:'Seventeen',18:'Eighteen',19:'Ninteen',20:'Twenty',30:'Thirty',40:'Fourty',50:'Fifty',60:'Sixty',70:'Seventy',80:'Eighty',90:'Ninty'}
if (num<20):
return d1[num]
if (num<100):
if num % 10 == 0:
return d1[num]
else:
return d1[num // 10 * 10] + ' ' + d1[num % 10]
if (num < 0):
return "This number is less than 0 and it's spelling is" + word(num)
print (word(- 100))
print (word(13))
You should have your wide condition before narrow condition.
In your code, you have 3 if conditions, num < 20, num < 100, num < 0, which actually is 0 <= num < 20, 20 <= num < 100, num < 0. The last condition is the widest, but you move it incorrectly at the bottom.
Sort your condition train into the order num < 0, num < 20, num < 100 may fix this issue.
Update: You can't use word[num] in your num < 0 block. I can't understand what "is one hundred" in your expected output. Is it a hardcoded text? Then hardcode it, for example:
def word(num):
d1= {0:'Zero',1:'One',2:'Two',3:'Three',4:'Four',5:'Five',6:'Six',7:'Seven',8:'Eight',9:'Nine',10:'Ten',11:'Eleven',12:'Twelve',13:'Thirteen',14:'Fourteen',15:'Fifteen',16:'Sixteen',17:'Seventeen',18:'Eighteen',19:'Ninteen',20:'Twenty',30:'Thirty',40:'Fourty',50:'Fifty',60:'Sixty',70:'Seventy',80:'Eighty',90:'Ninty'}
if num < 0:
return "This number is less than 0 and it's spelling is one hundred"
if num < 20:
return d1[num]
if num < 100:
if num % 10 == 0:
return d1[num]
else:
return d1[num // 10 * 10] + ' ' + d1[num % 10]
print(word(-100))
print(word(13))

Sequence function error?

I'm getting an error on line 3 "TypeError: 'int' object is not iterable," and its been bothering me. Any advice/fixes appreciated.
Example test: collatz_counts(4) → 3 # 4 -> 2 -> 1 (3 steps)
Code I have:
def collatz_counts(x):
num = 0
for i in (x):
if i == 1:
num += 1
return num
elif i % 2 == 0:
num(i) / 2
num += 1
num.append(i)
else:
num = (i*2) + 3
num += 1
num.append(i)
return num
This can be solved recursively:
def collatz_length(n):
if n == 1:
return 1
return 1 + collatz_length(3*n+1 if n%2 else n//2)
Which lends itself to be memoized if you are going to be calling for a range of numbers, e.g. in Py3
import functools as ft
#ft.lru_cache(maxsize=None)
def collatz_length(n):
if n == 1:
return 1
return 1 + collatz_length(3*n+1 if n%2 else n//2)
Which will run through the first million collatz sequences in about 2.31s vs about 28.6s for the iterative solution.
Use a while loop. Just modify x in place until you get to 1 and keep track of the number of steps each time you run a cycle.
def collatz_counts(x):
steps = 0
while x != 1:
if x % 2:
x = x * 3 + 1
else:
x = x // 2
steps += 1
return steps

Support with Logic Gate Simulation program in Python

I'm trying to create a logic gate simulation program in python so that a user can choose the type of logic gate they want to simulate. Once chosen, they can then enter the inputs and the program should return the value of the output from the chosen logic gate to the user.
This is what I have so far:
print ("Logic Gate Simulation")
def AND (a,b):
if a == 1 and b == 1:
return 1
else:
return 0
def NAND (a,b):
if a == 1 and b == 1:
return 0
else:
return 1
def OR(a,b):
if a == 1:
return 1
elif b == 1:
return 1
else:
return 0
def NOR (a,b):
if a != b:
return 1
else:
return 1
def Main():
question = input("what type of gate do you want to simulate - OR, AND or NAND? ")
if question == 'AND':
a = input("enter value for input 1")
b = input("enter value for input 2")
x= AND(a,b)
print (x)
else:
print ("")
Main()
When I run the program and enter AND, inputs 1 and 1 it still returns 0 and I cant see why.
this works:
def AND (a,b):
a=int(a)
b=int(b)
if a == 1 and b == 1:
return 1
else:
return 0
you have to tell python that a and b are integers - (there are better ways of doing that than the method shown)
This is my version of the code (I had to this for homework for AS Level, this may help you!) :
print("Logic Gate Calculator")
def AND(a, b): # AND Gate
a = int(a)
b = int(b)
if a == 1 and b == 1: # AND Gate logic
return 1
else:
return 0
def NAND(a, b): # NAND Gate
a = int(a)
b = int(b)
if a == 1 and b == 1: # NAND Gate logic
return 0
elif a == 1 and b == 0:
return 0
elif a == 0 and b == 1:
return 0
else:
return 1
def OR(a, b): # OR Gate
a = int(a)
b = int(b)
if a == 1: # OR Gate Logic
return 1
elif b == 1:
return 1
else:
return 0
def NOR(a, b): # NOR Gate
a = int(a)
b = int(b)
if a == 1 and b == 0: # NOR Gate Logic
return 1
elif a == 0 and b == 1:
return 1
elif a == 0 and b == 0:
return 1
else:
return 0
def XOR(a, b): # XOR Gate
a = int(a)
b = int(b)
if a == 1 and b == 0: # XOR Gate Logic
return 1
elif a == 1 and b == 1:
return 1
else:
return 0
def main(): # The main program
run = True
while run: # While loop
question = input("What type of gate do you want to use OR, AND, NOR, or NAND or (Q)uit") # Logic Gate chooser
if question == "AND" or question == "and" or question == "And": # If the user selects AND
a = input("Enter value for input 1 (1 or 0):") # Getting the Logic Gate's 1st input
b = input("Enter value for input 2 (1 or 0):") # Getting the Logic Gate's 2nd input
x = AND(a, b) # Calling the AND Function
print("The output will be:", x) # Output result
elif question == "OR" or question == "or" or question == "Or": # If the user selects OR
a = input("Enter value for input 1 (1 or 0):") # Getting the Logic Gate's 1st input
b = input("Enter value for input 2 (1 or 0):") # Getting the Logic Gate's 2nd input
x = OR(a, b) # Calling the OR Function
print("The output will be:", x) # Output result
elif question == "NOR" or question == "nor" or question == "Nor": # If the user selects NOR
a = input("Enter value for input 1 (1 or 0):") # Getting the Logic Gate's 1st input
b = input("Enter value for input 2 (1 or 0):") # Getting the Logic Gate's 2nd input
x = NOR(a, b) # Calling the NOR function
print("The output will be:", x) # Output result
elif question == "NAND" or question == "nand" or question == "Nand": # If the user selects NAND
a = input("Enter value for input 1 (1 or 0):") # Getting the Logic Gate's 1st input
b = input("Enter value for input 2 (1 or 0):") # Getting the Logic Gate's 2nd input
x = NAND(a, b) # Calling the NAND function
print("The output will be:", x) # Output result
elif question == "XOR" or question == "xor" or question == "Xor": # If the user selects XOR
a = input("Enter value for input 1 (1 or 0):") # Getting the Logic Gate's 1st input
b = input("Enter value for input 2 (1 or 0):") # Getting the Logic Gate's 2nd input
x = XOR(a, b) # Calling the XOR function
print("The output will be:", x) # Output result
elif question == "Q" or question == "q": # Quiting the program
run = False
else:
print("Please enter one of the shown logic gates") # Error handling
main()
a=True
b=True
output=a and b
print (output)
a=True
b=False
output=a and b
print (output)
Please try this
It works. Use
if a == 1:
if b == 1:
return 1
else
return 0