473,386 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Loops and checking for string starts

I've started with Python recently, reading a book.
Unfortunately, I've come through an error I do now know how to resolve.

If you could please help me, it would be greatly appreciated.

Expand|Select|Wrap|Line Numbers
  1. from random import randrange
  2.  
  3. #Simulates a fight
  4.  
  5. monsters = [
  6.     'Goblin',
  7.     'Orc',
  8.     'Human',
  9.     'Soldier',
  10.     'Fighter',
  11.     'Night Elf'
  12.     ]
  13.  
  14. rnd = randrange(0, 5)
  15. x = monsters[rnd]
  16.  
  17. i = 0
  18. while i <= range(len(monsters)):
  19.     if monsters[i].startswith('a' | 'e' | 'i' | 'u' | 'o'):
  20.        an = 'an '
  21.        i += 1
  22.     else:
  23.        an = 'a '
  24.        i += 1
  25.  
  26. print "You are fighting " + an + x
That is the code, that will (in the future) simulate a fight. At the moment, what I am trying to do is check if the monster starts with a vowel so that instead of "a Orc" it says "an Orc", which is correct.

I have tried with these ( | ) signs and with the or keyword, but none of them worked.
Dec 21 '09 #1
3 1889
In Python 2.5 and above, you can use a tuple as an argument to startswith:

>>> a = 'hello'
>>> a.startswith(('hel', 'he', 'no'))
True
>>>

Hope this helps.

Carl T.
Dec 21 '09 #2
In fact, your answer did help me (however it had nothing to do with my question), because your arguments reminded me somehow of a list, and remembered the possibility of turning a word into a list.

Then, it was easy to check for this list's first letter, and see if it was a vowel or not.

For anyone interested, this is the final code:

Expand|Select|Wrap|Line Numbers
  1. from random import randrange
  2.  
  3. #Simulates a fight
  4.  
  5. monsters = [
  6.     "Goblin",
  7.     "Orc",
  8.     "Human",
  9.     "Soldier",
  10.     "Fighter",
  11.     "Night Elf"
  12.     ]
  13.  
  14. rnd = randrange(0, 5)
  15. x = monsters[rnd]
  16. xList = list(x)
  17.  
  18. an = 'a '
  19. if xList[0] == 'a' or 'e' or 'i' or 'o' or 'u': an = 'an '
  20.  
  21. print "You are fighting " + an + x
  22.  
Dec 21 '09 #3
bvdet
2,851 Expert Mod 2GB
Using the "in" operator would be appropriate in your case.
Expand|Select|Wrap|Line Numbers
  1. for monster in monsters: 
  2.     print "You are fighting %s %s" % \
  3.           (["a", "an"][monster[0].lower() in \
  4.                        ['a', 'e', 'i', 'o', 'u'] or 0], monster)
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> You are fighting a Goblin
  2. You are fighting an Orc
  3. You are fighting a Human
  4. You are fighting a Soldier
  5. You are fighting a Fighter
  6. You are fighting a Night Elf
  7. >>> 
Dec 22 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: - ions | last post by:
I have created a JComboBox with its Items as a list of "M" numbers ie. M1,M2,M3.......throgh too M110 (thes are the messier objects, a catolouge of deep sky objects) the user selects of of these...
3
by: Oleg Leschov | last post by:
Could there be means of exiting nested loops in python? something similar to labelled loops in perl.. I consider it irrating to have to make a flag for sole purpose of checking it after loop if...
4
by: dw | last post by:
Hello all. We're doing a site with teams and their members. We've got a page where we need to display people according to who belongs to a which team. I've heard that nested loops are bad, but...
15
by: JustSomeGuy | last post by:
I have a need to make an applicaiton that uses a variable number of nested for loops. for now I'm using a fixed number: for (z=0; z < Z; ++z) for (y=0; y < Y; ++y) for (x=0; x < X; ++x)
67
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. ...
2
by: Rob O'Cop | last post by:
Hi, I've got an Intranet site that's been using the usual Forms authentication. Now, I want to retrieve the Windows Login and authenticate the user automatically without her typing...
1
by: gordon | last post by:
Hi I have some code that will prepare a text file that can be read by another application. The code is supposed to collect information on the starting point of a range, then end point of the...
5
by: Joe Van Dyk | last post by:
I have a file that looks like this: 25.000000 55.000000 23.000000 51.000000 5 8 8700 6000 <and then, at byte 128, starts binary stuff> What's the proper way to make sure that I'm reading in...
9
by: chutsu | last post by:
hi I got a simple program, and I was wondering how do you check if the string in an array = a string. For example if I put "APPLE" in array Array then how can I check it with a if statement. if...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.