foo =['foo','bar']for i in foo:print(i)#outputs 'foo' then 'bar'for i inrange(len(foo)):print(foo[i])#outputs 'foo' then 'bar'
i =0while i <len(foo):print(foo[i])#outputs 'foo' then 'bar'
num =[int(d)for d instr(input("Enter the number:"))]
even,odd =0,0for i inrange(0,len(num)):if i %2==0:
even = even + num[i]else:
odd = odd + num[i]print(abs(odd-even))# code contributed by Shubhanshu Arya PrepInsta Placement Cell Student
array =['cheese','milk','bread']for i in array:print(i)# will print out 'cheese' 'milk' and 'bread' to the console
i =0while i < array:print(array[i])#will print out 'cheese' 'milk' and 'bread' to the consolefor i inrange(len(array))print(i)#will print out 'cheese' 'milk' and 'bread' to the console#variable doesn't have to be i in for loop for example:for ingredients in array:print(i)#will print out 'cheese' 'milk' and 'bread' to the console#this can help you understand what the for loop is doing better
print(" Welcome to the 100 game
")print("To start the game you have to enter a number between 1 to 10")print("To end the game you have to reach the number 100")print("First one reach 100 win
")print("Good luck
")
nums =0# Display numbersdefdisplay_state():global nums
print("100/",nums)# Get number the player wants to playdefget_input(player):
valid =Falsewhilenot valid:# Repeat until a valid move is entered
message = player +" player please enter the number between 1 and 10: "
move =input(message)# Get move as stringif move.isdigit():# If move is a number
move =int(move)# can take 1-10 number onlyif move inrange(1,11)and nums + move <=100:
valid =Truereturn move
# Update numbers after the movedefupdate_state(nums_taken):global nums
nums += nums_taken
# Check if he/she is taking the last move and losesdefis_win():global nums
if nums >99:returnTrue# define the 100 gamedefplay__100_game():
display_state()while(True):# Repeat till one of them loses
first = get_input("First")
update_state(first)
display_state()# Display new numbersif(is_win()):print("First player won")break
second = get_input("Second")
update_state(second)
display_state()if(is_win()):print("Second player won")break
play__100_game()
print(" Welcome to the 100 game
")print("To start the game you have to enter a number between 1 to 10")print("To end the game you have to reach the number 100")print("First one reach 100 win
")print("Good luck
")
nums =0# Display numbersdefdisplay_state():global nums
print("100/",nums)# Get number the player wants to playdefget_input(player):
valid =Falsewhilenot valid:# Repeat until a valid move is entered
message = player +" player please enter the number between 1 and 10: "
move =input(message)# Get move as stringif move.isdigit():# If move is a number
move =int(move)# can take 1-10 number onlyif move inrange(1,11)and nums + move <=100:
valid =Truereturn move
# Update numbers after the movedefupdate_state(nums_taken):global nums
nums += nums_taken
# Check if he/she is taking the last move and losesdefis_win():global nums
if nums >99:returnTrue# define the 100 gamedefplay__100_game():
display_state()while(True):# Repeat till one of them loses
first = get_input("First")
update_state(first)
display_state()# Display new numbersif(is_win()):print("First player won")break
second = get_input("Second")
update_state(second)
display_state()if(is_win()):print("Second player won")break
play__100_game()
print(" Welcome to the 100 game
")print("To start the game you have to enter a number between 1 to 10")print("To end the game you have to reach the number 100")print("First one reach 100 win
")print("Good luck
")
nums =0# Display numbersdefdisplay_state():global nums
print("100/",nums)# Get number the player wants to playdefget_input(player):
valid =Falsewhilenot valid:# Repeat until a valid move is entered
message = player +" player please enter the number between 1 and 10: "
move =input(message)# Get move as stringif move.isdigit():# If move is a number
move =int(move)# can take 1-10 number onlyif move inrange(1,11)and nums + move <=100:
valid =Truereturn move
# Update numbers after the movedefupdate_state(nums_taken):global nums
nums += nums_taken
# Check if he/she is taking the last move and losesdefis_win():global nums
if nums >99:returnTrue# define the 100 gamedefplay__100_game():
display_state()while(True):# Repeat till one of them loses
first = get_input("First")
update_state(first)
display_state()# Display new numbersif(is_win()):print("First player won")break
second = get_input("Second")
update_state(second)
display_state()if(is_win()):print("Second player won")break
play__100_game()
print(" Welcome to the 100 game
")print("To start the game you have to enter a number between 1 to 10")print("To end the game you have to reach the number 100")print("First one reach 100 win
")print("Good luck
")
nums =0# Display numbersdefdisplay_state():global nums
print("100/",nums)# Get number the player wants to playdefget_input(player):
valid =Falsewhilenot valid:# Repeat until a valid move is entered
message = player +" player please enter the number between 1 and 10: "
move =input(message)# Get move as stringif move.isdigit():# If move is a number
move =int(move)# can take 1-10 number onlyif move inrange(1,11)and nums + move <=100:
valid =Truereturn move
# Update numbers after the movedefupdate_state(nums_taken):global nums
nums += nums_taken
# Check if he/she is taking the last move and losesdefis_win():global nums
if nums >99:returnTrue# define the 100 gamedefplay__100_game():
display_state()while(True):# Repeat till one of them loses
first = get_input("First")
update_state(first)
display_state()# Display new numbersif(is_win()):print("First player won")break
second = get_input("Second")
update_state(second)
display_state()if(is_win()):print("Second player won")break
play__100_game()