I want to write the other card's values in the same line with 'else'. E.g card number 2 equals 2, card number 3 equals 3...
btw from 1 to 13 is from ace to king and 15, 16, 17, 18 are suites.
def get_value(num, suit):
if (suit == 18 or suit == 15) and num == 7:
return -21
elif suit == 16 and num == 7:
return -11
elif num == 1 and (suit== 15 or suit==16 or suit==17 or suit==18):
return 11
elif (num == 12 or num == 13) and (suit== 15 or suit==16 or suit==17 or suit==18):
return 10
elif num == 11 and (suit== 15 or suit==16 or suit==17 or suit==18):
return 25
If I understand correctly you want to return the value of the card, where ace, jack, queen and king and 7 of a particular suit returns special values, otherwise just the numeric value.
Assuming the 15-18 are the only valid suits we do not need to check if the suit is one of those if all return the same value, we only need to treat the special cases.
def get_value(num, suit):
if num==7 and suit in (15,18): # suit is either 15 or 18
return -21
elif num==7 and suit==16:
return -11
elif num==1: # suit doesnt matter for ace
return 11
elif num in (12, 13): # queen or king
return 10
elif num==11: # ace
return 25
else: # all other cases - return num as value
return num
I'm guessing the final else statement is what you were looking for, where the value you get out is just the number on the card in in the general case where none of the special cases trigger, eg get_value(3, 15) would return 3.
I think this is what you want:
def get_value(num, suit):
return -21 if (suit == 18 or suit == 15) and num == 7 else -11 if suit == 16 and num == 7 else 11 if num == 1 and (suit== 15 or suit==16 or suit==17 or suit==18) else 10 if (num == 12 or num == 13) and (suit== 15 or suit==16 or suit==17 or suit==18) else 25 if num == 11 and (suit== 15 or suit==16 or suit==17 or suit==18) else None
Because this is very messy and chaotic, I think checking against a dict of constraints would be better:
def get_value(num, suit):
constraints = {((18, 15), (7,)): -21, ((16,), (7,)): -11, ((15, 16, 17, 18), (1,)): 11, ((15, 16, 17, 18), (12, 13)): 10, ((15, 16, 17, 18), (11,)): 25}
for x in constraints:
if suit in x[0] and num in x[1]:
return constraints[x]
A constraint tuple (ex. ((18, 15), (7,)) ) is only fulfilled if its first tuple (ex. (18, 15) ) contains suit and its second tuple (ex. (7,) ) contains num.
Additionally you could return a default value if the args passed in don't match the constraints.
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...
Here's the definition of Modest Number.
A number n is called modest if its digits can be separated into two numbers a and b such that n divided by b gives a as remainder.
For example, 2851111 is modest because divided by 1111 gives 285 as remainder.
Here's an example which I considered:
is_modest(21333) ➞ True
Combination 1: Left = 2 | Right = 1333 21333 % 1333 = 5 != Left
Combination 2: Left = 21 | Right = 333 21333 % 333 = 21 = Left
At least a combination satisfies the condition
Below is what I tried:
def isModest(num):
numStr = str(num)
numLen = len(numStr)
while numLen > 1:
for i in range(1, numLen):
rem = num % (int(numStr[i:])) # 5, 21, 15, 0
if rem == int(numStr[:i]):
return True
else:
return False
print(isModest(2036)) <br>
print(isModest(3412)) <br>
print(isModest(21333)) <br>
print(isModest(8)) #??? <br>
You don't need to use string manipulations. Dividing by powers of 10 will do it more efficiently
def isModest(N):
if N<10: return "cannot split"
p10 = 10
while p10 < N:
a,b = divmod(N,p10)
if b and N%b == a: return True
p10 *= 10
return False
for n in range(1,100):
if isModest(n) is True: print(n)
13
19
23
26
29
39
46
49
59
69
79
89
so i'm creating this function that gets a user to input a number greater than 2.
The code should then print all the prime numbers starting from 2 and ending at the number the user inputted.
So here is my code and it works (yay!)
def enterNumber():
number = int(input("Enter a number greater than 2"))
lower = 2
upper = number
for number in range (lower, upper):
if number > 2:
for i in range (2, number):
if (number % i) == 0:
break
else:
if (number % i) != 0:
print(number)
enterNumber()
BUT here is my output if the user inputs 18
"Enter a number greater than 2"
user puts "18"
output: 3
5
5
5
7
7
7
7
7
9
11
11
11
11
11
11
11
11
11
13
13
13
13
13
13
13
13
13
13
13
15
17
17
17
17
17
17
17
17
17
17
17
17
17
17
17
why is my output repeating itself? Any suggestions would be greatly appreciated thanks!
Your problem is here:
for i in range (2, number):
if (number % i) == 0:
break
else:
if (number % i) != 0:
print(number)
The else being inside the loop, says that you're going to print the number every time you find a non-divisor. For instance, for number = 11, you will print 11 for i values 2-10.
You don't know whether the number is a prime until you are done with the loop. Your print has to go after the loop, on a normal exit. An often-forgotten language feature is that a loop can have an else for exactly this purpose.
for i in range (2, number):
if (number % i) == 0:
break
else:
print(number)
Output:
Enter a number greater than 218
3
5
7
11
13
17
A simple solution(there are better ways):
def enterNumber():
number = int(input("Enter a number greater than 2"))
lower = 2
upper = number
for number in range(lower, upper):
prime = True
for i in range(2, number):
if (number % i) == 0:
prime = False
break
if prime:
print(number)
enterNumber()
I've met a question about list comprehension.
num1 = [5,10,15]
num2 = [i**2 if i == 10 else i-5 if i < 7 else i+5 for i in num1]
why num2 is num2 = [0,100,20]?
How does the result get?
Read it as:
num2 = [i**2 if i == 10
else i-5 if i < 7
else i+5
for i in num1]
5 is not equal to 10, but it is less than 7, so it yields 5 - 5 (i.e. 0)
10 is equal to 10, so it yields 10 ** 2 (i.e. 100)
15 is not equal to 10, not equal to 7, thus we use the default case, so it yields 15 + 5 (i.e. 20)
This list comprehension is exactly equivalent to:
num2 = []
for i in num1:
if i == 10:
num2.append(i**2)
else:
if i < 7:
num2.append(i-5)
else:
num2.append(i+5)
I've created a program that successfully detects whether a number is prime, or not, it also will return a list of the factors of the number if it isn't, but that part is not successful.
Here is my code:
def prime_num():
num = int(input("Give me a number...: "))
prime = True
if num == 1:
prime = False
elif num == 2:
prime = True
for x in range(2, num):
if num % x == 0:
prime = False
break
if prime == False:
print("That's not a prime number!")
factors(num)
elif prime == True:
print("That's a prime number!")
def factors(num):
factors = []
for x in range(1, num+1):
if num % x == 0:
factors.append(x)
print("The factors for " + str(num) + " are: ", factors)
for x in factors:
for y in range(1, x):
if x % y == 0:
factors.remove(x)
print("The prime factors for " + str(num) + " are: ", factors)
When I use this function with a "num" value of 25 I get this output...
prime_num()
Give me a number...: 25
That's not a prime number!
The factors for 25 are: [1, 5, 25]
The prime factors for 25 are: [1, 25]
Which isn't the correct output for prime factors, I just want it to return: [5]
(I'm not concerned about the multiplicity of the factors at this time)
However, when I try the number 50, as my "num". I get this output with a valueError:
prime_num()
Give me a number...: 50
That's not a prime number!
The factors for 50 are: [1, 2, 5, 10, 25, 50]
Traceback (most recent call last):
File "<ipython-input-19-12c785465e2a>", line 1, in <module>
prime_num()
File "C:/Users/x/Desktop/Python/Python Practice/primes.py", line 25, in prime_num
factors(num)
File "C:/Users/x/Desktop/Python/Python Practice/primes.py", line 40, in factors
factors.remove(x)
ValueError: list.remove(x): x not in list
I realize this means somehow my x isn't in factors, but I'm not sure how considering I'm specifically iterating through factors.
This should make it clear what your problem is:
factors = [1,5,25]
for x in factors:
for y in range(1,x):
print x,y
5 1
5 2
5 3
5 4
25 1
25 2
25 3
25 4
25 5
25 6
25 7
25 8
25 9
25 10
25 11
25 12
25 13
25 14
25 15
25 16
25 17
25 18
25 19
25 20
25 21
25 22
25 23
25 24
You're iterating over your factors in such a way that you ignore 1 and ignore the x % x combination. range(1,1) is the empty list, and then you simply stop short because you've increased the start point by 1 (from zero) but not the end point, leaving what you iterate over too short.
The reason you get a ValueError is because any non-square number (ie, not 4, 9, 16, 25, etc.) will be removed twice. For 6, for example, it will remove for the 2,3 combo and when it gets to the 3,2 combo it's already been removed, thus the error. One way to fix this is to make the code only go halfway minus one to your total, so that inverted numbers aren't removed twice. For example, stop at 2 for 6, 4 for 10, etc.