The Artima Developer Community
Sponsored Link

Python Answers Forum
while / if

2 replies on 1 page. Most recent reply: Aug 20, 2004 9:13 PM by Greg Jorgensen

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Calvin

Posts: 3
Nickname: calvin79
Registered: Aug, 2004

while / if Posted: Aug 12, 2004 9:26 PM
Reply to this message Reply
Advertisement
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


cristi

Posts: 2
Nickname: cristip
Registered: Aug, 2004

Re: while / if Posted: Aug 17, 2004 7:00 AM
Reply to this message Reply
You have to work with Random module to generate your choice letter. Loop with a while(i< your number) and in each loop print to file or where you want to have your answer: print "chioce", i, "=", resolt from random generation
And choosing your leter from a list of string wouldn't be to hard.
I hope I undestood well what you wanted with your little program, if not you'd better rewrite what you wanted for everyone to understand.

Greg Jorgensen

Posts: 65
Nickname: gregjor
Registered: Feb, 2004

Re: while / if Posted: Aug 20, 2004 9:13 PM
Reply to this message Reply

# solution 1: for loop
import random

# choices can be any sequence type: string of characters, list, etc.
choices = 'abcdefghijklmnopqrstuvwxyz'

n = input('Number of things (1-8)?')

for i in range(1, n+1):
print 'choice %d = %s' % (i, random.choice(choices))

---

# solution 2: while loop
import random

choices = 'abcdefghijklmnopqrstuvwxyz'

n = input('Number of things (1-8)?')

i = 1
while i <= n:
print 'choice %d = %s' % (i, random.choice(choices))

---

see www.geekschool.org for more Python info and samples.

Greg Jorgensen
PDXperts LLC, Portland, Oregon USA

Flat View: This topic has 2 replies on 1 page
Topic: where should i start? Previous Topic   Next Topic Topic: dir(__builtin__)  and dir(

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use