my_iterable = [1,2,3]
for item_name in my_iterable:
print(item_name)
>>1
>>2
>>3
mylist = [1,2,3,4,5]
for num in mylist:
#Check for even
if num % 2 == 0:
print(num)
else:
print(f"Odd Number: {num}")
Odd Number: 1
2
Odd Number: 3
4
Odd Number: 5
Generate two sets of arrays representing the value of dice thrown by two dice players. For each throw, the larger number win the set. If the number thrown is the same, it is a draw.
import numpy as np
for x in range(5):
p1 = np.random.randint(6,size=5)
p2 = np.random.randint(6,size=5)
print("Player 1", "\n", p1)
print("Player 2", "\n", p2)
p1score = 0
p2score = 0
for y in range(5):
if p1[y] > p2[y]:
p1score += 1
elif p1[y] < p2[y]:
p2score += 1
if p1score > p2score:
print("Player 1 wins by", (p1score - p2score), "game")
elif p1score < p2score:
print("Player 2 wins by", (p2score - p1score), "game")
else:
print("It is a draw with", (p1score), "win each.")
Player 1
[3 1 5 1 0]
Player 2
[3 2 1 2 0]
Player 2 wins by 1 game
Player 1
[1 0 2 3 2]
Player 2
[4 5 2 3 4]
Player 2 wins by 3 game
Player 1
[5 1 1 5 0]
Player 2
[1 2 0 3 1]
Player 1 wins by 1 game
Player 1
[1 1 1 4 5]
Player 2
[0 4 0 4 2]
Player 1 wins by 2 game
Player 1
[3 4 5 4 1]
Player 2
[0 3 5 3 3]
Player 1 wins by 2 game
Create a function to determine if a given pass code is well-formed.
def check_validity(pw):
for word in pw:
if "#" in word:
position = word.index("#")
part1 = word[:position]
part2 = word[position+1:]
if ((part1.isalnum() or ("." in part1[1:-1]) == True) and (".." not in part1)):
if (part2.isalnum() == True) and len(set(part2)) == len(part2): #returns a boolean, True if the string has no repeating characters, False otherwise
print(word, "is valid")
else:
print(word, "is not valid")
else:
print(word, "is not valid")
else:
print(word, "is not valid")
cases = ['abc#abc', '1#patch', 'a.b#cd9', '12to13#timesup', 'a.b.c#987', '.ab#123', 'a..b#123', 'ab.#123',
'ab.c#1.23', '1234.abc', 'ab12#pass', 'ab-12#past', 'ab12#pas$']
check_validity(cases)
#{i for i in part2 if part2.count(i) == 1}
#(len(part2) == len(set(part2))
abc#abc is valid
1#patch is valid
a.b#cd9 is valid
12to13#timesup is valid
a.b.c#987 is valid
.ab#123 is not valid
a..b#123 is not valid
ab.#123 is not valid
ab.c#1.23 is not valid
1234.abc is not valid
ab12#pass is not valid
ab-12#past is not valid
ab12#pas$ is not valid