I'm making a program to get the amount of letters in a number:
def convert(number):
lettercount = 0
numstr = str(number)
# One's places
if len(numstr) is 1:
if number == 1 or number == 2 or number == 6:
lettercount += 3
elif number == 4 or number == 5 or number == 9:
lettercount += 4
else:
lettercount += 5
# Ten's places
elif len(numstr) is 2:
if number == 10:
lettercount += 3
elif number == 11 or number == 12:
lettercount += 6
elif number == 15 or number == 16:
lettercount += 7
elif number == 13 or number == 14 or number == 19:
lettercount += 8
elif number == 17 or number == 18:
lettercount += 9
elif number == 20 or number == 30 or number == 40 or\
number == 80 or number == 90:
lettercount += 6
else:
lettercount += convert(int((numstr)[-1]))
lettercount += convert(int(round(number, -1)))
return lettercount
print "88 has %i letters in its name." % convert(88)
print "23 has %i letters in its name." % convert(23)
print "46 has %i letters in its name." % convert(46)
It works just fine and returns a correct response for the 88 and 23, but it gives a recursion depth error on 46. I'm confused; why does it happen on just 46?
Fixed code:
def convert(number):
lettercount = 0
numstr = str(number)
# One's places
if len(numstr) == 1:
if number == 1 or number == 2 or number == 6:
lettercount += 3
elif number == 4 or number == 5 or number == 9:
lettercount += 4
else:
lettercount += 5
# Ten's places
elif len(numstr) == 2:
if number == 10:
lettercount += 3
elif number == 40 or number == 50:
lettercount += 5
elif number == 11 or number == 12 or number == 20 or number == 30 or\
number == 80 or number == 90:
lettercount += 6
elif number == 15 or number == 16:
lettercount += 7
elif number == 13 or number == 14 or number == 19:
lettercount += 8
elif number == 17 or number == 18:
lettercount += 9
else:
lettercount += convert(int((numstr)[-1]))
lettercount += convert((int(numstr) // 10) * 10)
return lettercount
print "88 has %i letters in its name." % convert(88)
print "23 has %i letters in its name." % convert(23)
print "46 has %i letters in its name." % convert(46)
Because when you do
convert(int(round(number, -1)))
you are calling convert(50). Since 50 isn't covered by your if statements, it gets to the else again, and calls convert(50) again, and so forth.
The problem here is that round(46, -1) will produce the value 50. When convert is called with the value 50 it will go to the exact same line
lettercount += convert(int(round(number, -1)))
The round(50, -1) call will produce 50 and at this point the convert function will execute infinitely
Related
So, I have a homework. As you see below there're some cards below and each has a name. they're numbered from 0 to 35 and from left top to bottom. User enters a number and program has to tell the name, color and the number of the card. But there're some rules for it.
I can only use nested if and operators. Can't use while or other functions.
I can use max 13 if
can't use 36 if, else swap
this is the image of cards
I am not sure about what to do after this...
'''
num = int(input('Enter a number: '))
if num == 0 or num == 1 or num == 2 or num == 3:
print("6")
if num == 4 or num == 5 or num == 6 or num == 7:
print('7')
if num == 8 or num == 9 or num == 10 or num == 11:
print('8')
if num == 12 or num == 13 or num == 14 or num == 15:
print('9')
if num == 16 or num == 17 or num == 18 or num == 19:
print('10')
if num == 20 or num == 21 or num == 22 or num == 23:
print('vale')
if num == 24 or num == 25 or num == 26 or num == 27:
print('queen')
if num == 28 or num == 29 or num == 30 or num == 31:
print('king')
if num == 32 or num == 33 or num == 34 or num == 35:
print('tus')
'''
Here is what you are looking for in one line:
print(([str(i) for i in range(6, 11)] + ['vale', 'queen', 'king', 'tus'])[(int(input('Enter a number: '))) // 4])
The goal is to create a list of avalaible cards. Then use euclidien division by 4 to select the right card...
I have a function that looks at the sales quantities of each product in a transaction and returns a value based on the quantity.
I need the function to work for any number of products in a transaction; presently the maximum is 6 different products in a transaction, but I want the function to work even if that increases to say 9 products.
I defined the function to compare up to 20 products, but I get a KeyError when applying it as columns 'Product_Quantity_7' onwards do not exist.
Could someone please share a method by which I can make the function robust to any number of products in my dataset (I've attached a snapshot of my dataset as well)?
def trial_function(row, df):
if row['Product_Quantity_1'] >= 6:
return 6
elif row['Product_Quantity_1'] >= 3:
return 3
elif row['Product_Quantity_2'] >= 6:
return 6
elif row['Product_Quantity_2'] >= 3:
return 3
elif row['Product_Quantity_3'] >= 6:
return 6
elif row['Product_Quantity_3'] >= 3:
return 3
elif row['Product_Quantity_4'] >= 6:
return 6
elif row['Product_Quantity_4'] >= 3:
return 3
elif row['Product_Quantity_5'] >= 6:
return 6
elif row['Product_Quantity_5'] >= 3:
return 3
elif row['Product_Quantity_6'] >= 6:
return 6
elif row['Product_Quantity_6'] >= 3:
return 3
elif row['Product_Quantity_7'] >= 6:
return 6
elif row['Product_Quantity_7'] >= 3:
return 3
elif row['Product_Quantity_8'] >= 6:
return 6
elif row['Product_Quantity_8'] >= 3:
return 3
elif row['Product_Quantity_9'] >= 6:
return 6
elif row['Product_Quantity_9'] >= 3:
return 3
Here's what my dataset looks like
I'm trying to print out each iteration but instead it's printing out the last value as many times as the input. How do I fix it?
Code 1:
for i in range (1,n+1):
if n % 3 == 0 and n % 5 != 0:
print("Fizz")
if n % 5 == 0 and n % 3 != 0:
print("Buzz")
if n % 3 == 0 and n % 5 == 0:
print("FizzBuzz")
If I input 15, for example, it prints out "FizzBuzz" 15 times. I want it to print out something like this:
Sample Output:
1, 2, Fizz, 4, ..., Fizzbuzz
All you need are a couple minor modifications:
for i in range(1, n + 1):
if i % 3 == 0 and i % 5 != 0:
print("Fizz")
elif i % 5 == 0 and i % 3 != 0:
print("Buzz")
elif i % 3 == 0 and i % 5 == 0:
print("FizzBuzz")
else:
print(i)
You were making the mathematical tests against n, not i.
What's the error here?. I cant print the CozaLoza, CozaWoza and LozaWoza. And it only prints from 1-109. I also wants to print 11 output per lines. How can I do that?
for num in range (1, 22):
if (num % 3 == 0):
print("Coza" )
elif (num % 5 == 0):
print("Loza")
elif (num % 7 == 0):
print("Woza")
elif (num % 3 and num % 5 == 0):
print(" CozaLoza")
elif (num % 3 and num % 7 == 0):
print("CozaWoza")
elif (num % 5 and num % 7 == 0):
print("LozaWoza")
else :
print (num)
I updated the code as following:
num = int ( input ( "Enter a number: " ) )
if num < 1 or num > 110:
print("From 1-110 number is allowed")
else :
for n in range ( 1, num ) :
name = str(n)
if n % 3 == 0:
name = "Coza"
elif n % 5 == 0:
name = "Loza"
elif n % 7 == 0:
name = "Woza"
print(name, end=" ")
if (n + 1) % 10 == 0:
print("") # print new line each 10 number prints
The answer for your question is the use of if (n+1) % 10 == 0 and under.
It prints newline for each 10 numbers because of every mupltiple of 10 is True on (n+1) % 10 == 0.
BUT it has some wrongs on other lines.
NOT the for num in range(1, num), it is for n in range(1, num). the num is same with a name of variable in loops and it is ambiguous.
you could omit bracket ( and ) at if, elif statement in Python. if (A) equals if A in python :)
anyway, this is the output when input is 55:
Enter a number: 55
1 2 Coza 4 Loza Coza Woza 8 Coza
Loza 11 Coza 13 Woza Coza 16 17 Coza 19
Loza Coza 22 23 Coza Loza 26 Coza Woza 29
Coza 31 32 Coza 34 Loza Coza 37 38 Coza
Loza 41 Coza 43 44 Coza 46 47 Coza Woza
Loza Coza 52 53 Coza
EDITED: (by updated question)
when num is 10, the code can be reached to elif num % 3 and num % 5 == 0? I don't think so. because it was already True on elif num % 5 == 0 above.
you need to fix your if conditions as like:
if num % 3 == 0:
...
elif num % 5 == 0:
if num % 3:
# CozaLoza
else:
# Loza
This code is supposed to output fizz if number is divisible by 3, buzz if divisible by 5 and fizzbuzz if divisible by 3 and 5. Although I'm a bit unfamiliar with defining my own function and using the return appropriately. How do I remove the last 16 if the user inputs the number 16?
number = int(input("Enter a number: "))
def fizzbuzz(number):
n = 1
while n <= number:
if n % 3 != 0 and n % 5 != 0:
print(n)
elif n % 3 == 0 and n % 5 == 0:
print("fizzbuzz")
elif n % 3 == 0:
print("fizz")
elif n % 5 == 0:
print("buzz")
n = n + 1
return number
print(fizzbuzz(number))
If number = 16 it outputs
Enter a number: 16
1
2
fizz
4
buzz
fizz
7
8
fizz
buzz
11
fizz
13
14
fizzbuzz
16
16
How do I get I remove the last number 16, as it isn't supposed to be there
print("buzz")
n = n + 1
return number
print(fizzbuzz(number))
Here's your problem. Don't return the number, and don't print the return value of the function.
print("buzz")
n = n + 1
fizzbuzz(number)