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...
Related
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
I am new to Python and Stackoverflow in general, so sorry if my formatting sucks and i'm not good at enlish.But i have a problem with this code.
print('Displays prime numbers from 1 to N.')
n = int(input('Please enter a value of n: '))
for n in range(1, n + 1):
if n >= 1:
for i in range(2, n):
if (n % i) == 0:
break
else:
print('They are',n,end=' ')
The result of the code when ran comes out looking like this:
Displays prime numbers from 1 to N.
Please enter a value of n:40
They are 1 They are 2 They are 3 They are 5 They are 7 They are 11 They are 13 They are 17 They are 19 They are 23 They are 29 They are 31 They are 37
but i want it like this:
Displays prime numbers from 1 to N.
Please enter a value of n:40
They are 1 2 3 5 7 11 13 17 19 23 29 31 37
If you're completely determined not to use the print function more than once inside the loop, you could set a flag to determine whether to print the first two words. Like so:
print('Displays prime numbers from 1 to N.')
n = int(input('Please enter a value of n: '))
first = 'They are '
for n in range(1, n + 1):
if n >= 1:
for i in range(2, n):
if (n % i) == 0:
break
else:
print(first + str(n), end=' ')
if len(first) > 0:
first = ''
The following solution may help you
print('Displays prime numbers from 1 to N.')
n = int(input('Please enter a value of n: '))
num = [] # Create empty list
for n in range(1, n + 1):
if n >= 1:
for i in range(2, n):
if (n % i) == 0:
break
else:
num.append(n)
# Write the print statement outside of the loop and use .join() function and for loop
#to print each element of the list look like the output you have posted
#
print('They are'," ".join(str(x) for x in num))
Output:
Displays prime numbers from 1 to N.
Please enter a value of n: 40
They are 1 2 3 5 7 11 13 17 19 23 29 31 37
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)
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