I'm doing a little program. This program works as follows:
1- U put how many coins do u want to have
2- U put all of them 'heads'
3- From the second coin, you start to flip the coins that the index a multiple of 2, when u finish you go back to the start and start again, but this time you'll start from the third coin and flip all that the index is multiple of 3, and continue like that, restart from the fourth coin and flip the multiples of 4 and continue until the end...
Example: 4 coins--> heads, heads,heads,heads
1° time: heads,talls,heads,talls,
2° time: heads,talls,talls,talls,
3° time: heads, talls,talls,heads.
I think this code is almost reaching it, but this function I built doesn't do the job of flipping the coin and change the 'mode' on the 'moedas' list
Vocab: Heads-cara / Talls - coroa /Coin(s)- moeda(s) /To flip - Virar (portuguese)
I'll apreciate any help to change some error on the rest of the code too.
def Virar(moeda):
if moeda == 'cara':
moeda = 'coroa'
if moeda == 'coroa':
moeda = 'cara'
return moeda
qtde_moedas = int(input('How much coins do u want?'))
moedas=[]
while (len(moedas))<qtde_moedas:
moedas.append('cara')
for divisor in range(2,len(moedas)):
for index_num in range(len(moedas)):
if (index_num 1)%divisor==0:
moedas[index_num] = Virar(moedas[index_num])
else:
pass
CodePudding user response:
Say you pass 'cara'
to your function Virar
; first it changes moeda
to 'coroa'
, because it was 'cara'
. And then, it checks whether moeda
is 'coroa'
, which is True
now, so it turns it back to 'cara'
! So Viara
never flips the coin as you wish. Instead, try using elif
or else
:
def Virar(moeda):
if moeda == 'cara':
moeda = 'coroa'
else:
moeda = 'cara'
return moeda
A shorter version, if you are interested, would be the following:
def virar(moeda):
return 'coroa' if moeda == 'cara' else 'cara'
qtde_moedas = int(input("How many coins do you want? "))
moedas = ['cara'] * qtde_moedas
for divisor in range(2, qtde_moedas 1):
for i in range(divisor - 1, qtde_moedas, divisor):
moedas[i] = virar(moedas[i])
print(moedas)