while and if

C

Calvin79

Hi,

I'm very new to python, but believe in learning from examples. So could
someone please help me with this?

I choose a number of 'things' I want

Number = input("Number of things (1-8)? ")
things = ['1', '2', '3', '4', '5', '6', '7',\
'8']

From there I want to take at random a letter and do this n times depending
on the number I gave.

choice = choice('abcd')

The final answer if say the number had been 5 to look something like;

choice 1 = d
choice 2 = c
choice 3 = a
choice 4 = d
choice 5 = b
etc...

I can sort of see how to use the while loop for int but not to keep
choosing from the list and lable it choice 1 etc...

Thanks for any help,

Calvin
 
S

Satchidanand Haridas

Calvin79 said:
Hi,

I'm very new to python, but believe in learning from examples. So could
someone please help me with this?

I choose a number of 'things' I want

Number = input("Number of things (1-8)? ")
things = ['1', '2', '3', '4', '5', '6', '7',\
'8']
From there I want to take at random a letter and do this n times depending
on the number I gave.

choice = choice('abcd')

The final answer if say the number had been 5 to look something like;

choice 1 = d
choice 2 = c
choice 3 = a
choice 4 = d
choice 5 = b
etc...

I can sort of see how to use the while loop for int but not to keep
choosing from the list and lable it choice 1 etc...

Thanks for any help,

Calvin
The following code does what I think you want to do:

<code>
import random

things = range(int(raw_input("Number of things (1-8)? ")))

for x in things:
print "choice ",x+1, " is ", random.choice('abcd')

</code>

Regards,
Satchit


----
Satchidanand Haridas (sharidas at zeomega dot com)

ZeOmega (www.zeomega.com)
Open Minds' Open Solutions

#20,Rajalakshmi Plaza,
South End Road,
Basavanagudi,
Bangalore-560 004, India
 
C

Calvin79

Hi Satchit,

Can I also ask how could I stop two of the same letters being given i.e.
aa or dd and also how would I stop the same letter being repeated with
only one space between i.e. a b a or c a c

Thanks,

Calvin
 
S

Satchidanand Haridas

Calvin79 said:
Hi Satchit,

Can I also ask how could I stop two of the same letters being given i.e.
aa or dd and also how would I stop the same letter being repeated with
only one space between i.e. a b a or c a c

Thanks,

Calvin

Hi,

You will have to maintain the previous two states to prevent the letters
repeating within the next two choices. The code below will do something
like that. I don't know if it is the best solution:

<code>

import random

things = xrange(int(raw_input("choose no of things (1-8)? ")))

state = [None,None]

for x in things:
tmp = random.choice('abcd')
print state
while tmp in state[0:2]:
tmp = random.choice('abcd')
print "choice ",x+1," is ", tmp
state[x%2] = tmp

</code>

Regards,
Satchit
 
C

Calvin79

Hi Satchit,

Thankyou for what you've already done, but would you mind if I ask one
last thing and then I'll leave you alone?

In it's present form the output is fine, but I think I might like to have
it output in this format;['c', 'b', 'a', 'd']. It is probably the most
basic of questions to ask, but heck I've only been at this python stuff a
week... : )

Calvin
 
S

Satchidanand Haridas

Calvin79 said:
Hi Satchit,

Thankyou for what you've already done, but would you mind if I ask one
last thing and then I'll leave you alone?

In it's present form the output is fine, but I think I might like to have
it output in this format;['c', 'b', 'a', 'd']. It is probably the most
basic of questions to ask, but heck I've only been at this python stuff a
week... : )

Calvin
Hi,

Change the 'print' line to append to a list:

<code>

import random

things = xrange(int(raw_input("choose no of things (1-8)? ")))

state = [None,None]

l = [] # new line

for x in things:
tmp = random.choice('abcd')
print state
while tmp in state[0:2]:
tmp = random.choice('abcd')
print "choice ",x+1," is ", tmp
l.append(tmp) # new line
state[x%2] = tmp

print l # new line

</code>

----
Satchidanand Haridas (sharidas at zeomega dot com)

ZeOmega (www.zeomega.com)
Open Minds' Open Solutions

#20,Rajalakshmi Plaza,
South End Road,
Basavanagudi,
Bangalore-560 004, India
 
S

Satchidanand Haridas

Hi,

There was a formatting error in my last mail. In the code, the
'l.append(temp)' should appear on a new line. So:

<correction>

print "choice ",x+1," is ", tmp
l.append(temp)

</correction>

Regards,
Satchit

----
Satchidanand Haridas (sharidas at zeomega dot com)

ZeOmega (www.zeomega.com)
Open Minds' Open Solutions

#20,Rajalakshmi Plaza,
South End Road,
Basavanagudi,
Bangalore-560 004, India



Satchidanand said:
Hi Satchit,

Thankyou for what you've already done, but would you mind if I ask one
last thing and then I'll leave you alone?

In it's present form the output is fine, but I think I might like to
have
it output in this format;['c', 'b', 'a', 'd']. It is probably the most
basic of questions to ask, but heck I've only been at this python
stuff a
week... : )
Calvin
Hi,

Change the 'print' line to append to a list:

<code>

import random

things = xrange(int(raw_input("choose no of things (1-8)? ")))

state = [None,None]

l = [] # new line

for x in things:
tmp = random.choice('abcd')
print state
while tmp in state[0:2]:
tmp = random.choice('abcd')
print "choice ",x+1," is ", tmp

l.append(tmp) # new line
state[x%2] = tmp

print l # new line

</code>

----
Satchidanand Haridas (sharidas at zeomega dot com)

ZeOmega (www.zeomega.com)
Open Minds' Open Solutions

#20,Rajalakshmi Plaza,
South End Road,
Basavanagudi,
Bangalore-560 004, India
 
C

Calvin79

Satchit,

You've been great, saved me a lot of time and probably stopped me going
bald as I won't be scratching my head half as much trying to get to grips
with all this.

Once again thankyou!

Calvin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top