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. - from random import randrange
-
-
#Simulates a fight
-
-
monsters = [
-
'Goblin',
-
'Orc',
-
'Human',
-
'Soldier',
-
'Fighter',
-
'Night Elf'
-
]
-
-
rnd = randrange(0, 5)
-
x = monsters[rnd]
-
-
i = 0
-
while i <= range(len(monsters)):
-
if monsters[i].startswith('a' | 'e' | 'i' | 'u' | 'o'):
-
an = 'an '
-
i += 1
-
else:
-
an = 'a '
-
i += 1
-
-
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.
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.
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: -
from random import randrange
-
-
#Simulates a fight
-
-
monsters = [
-
"Goblin",
-
"Orc",
-
"Human",
-
"Soldier",
-
"Fighter",
-
"Night Elf"
-
]
-
-
rnd = randrange(0, 5)
-
x = monsters[rnd]
-
xList = list(x)
-
-
an = 'a '
-
if xList[0] == 'a' or 'e' or 'i' or 'o' or 'u': an = 'an '
-
-
print "You are fighting " + an + x
-
bvdet 2,851
Expert Mod 2GB
Using the "in" operator would be appropriate in your case. - for monster in monsters:
-
print "You are fighting %s %s" % \
-
(["a", "an"][monster[0].lower() in \
-
['a', 'e', 'i', 'o', 'u'] or 0], monster)
Output: - >>> You are fighting a Goblin
-
You are fighting an Orc
-
You are fighting a Human
-
You are fighting a Soldier
-
You are fighting a Fighter
-
You are fighting a Night Elf
-
>>>
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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)
|
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. ...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
| |