0

So I'm pretty sure I've done this completely wrong but I don't know how to do it. I was planning to use an array but don't know how. I'm pretty new to the less fluffy-side of Python (Learning it at school) So if I could have a hand with this it would be much appreciated.

So I'm trying to get some practise designing and programming, and I wanted to make a Pokémon type comparison using a chart like this:

http://i.kinja-img.com/gawker-media/image/upload/s--6gT1hiPW--/fxovveduxtomv4srnqk1.png

So, the idea is that I input the type(s) and it will output its strengths and weaknesses. But for the life of me I cannot get past the initial stage of selecting a type.

Here's my first couple of lines:

Elements = "Normal","Fire","Water","Grass","Electric","Bug","Flying","Ground","Rock","Posion","Dragon","Dark","Fairy","Psychic","Steel","Fighting","Ice"
type1 = input("Please input a type")
while type1 != Elements:
    type1 = input("Please input a real type")

print("Good Job, this part works!") # But it doesn't get to this point...

I'm sorry its so bad, but everyone starts off naïve right? Thank you in advance for any help you can give me!

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Obahar
  • 107
  • 1
  • 1
  • 10
  • 1
    don't capitalize a variable name. write `elements` instead – Yeo May 05 '15 at 13:22
  • `elements` is a list. use the following syntax to define a list `["apple", "banana"]` – Yeo May 05 '15 at 13:23
  • 2
    @Yeo `Elements` is a *tuple*, which is fine for the current purposes. If anything I would suggest a `set` rather than a `list`. – jonrsharpe May 05 '15 at 13:24
  • still bad practice, Python Zen: `Explicit is better then implicit`.use `("apple", "banana", "cherry", )` – Yeo May 05 '15 at 13:25
  • @Yeo per [the documentation](https://docs.python.org/2/library/stdtypes.html#sequence-types-str-unicode-list-tuple-bytearray-buffer-xrange) (emphasis mine): *"Tuples are constructed by the comma operator (not within square brackets), **with or without enclosing parentheses**"*. I agree that explicit is good, but the commas alone are sufficient syntax to indicate a non-empty tuple. – jonrsharpe May 05 '15 at 13:40

3 Answers3

2

First of all you need a list to store all the types in , and then repeatedly ask for user input then match the input against the pre defined list of elements, if a match is found you break the while loop else you just continue , That's the simple algorithm to be followed in this case.

elements = ["Normal","Fire","Water","Grass","Electric","Bug","Flying","Ground","Rock","Posion","Dragon","Dark","Fairy","Psychic","Steel","Fighting","Ice"]
#Initialized the various types in a list.
while True:    #Infinite loop
    type1 = input("Please input a real type")   #Taking input from the user
    if type1 in elements:    #Checking if the input is already present in the given list of elements.
        print("Good Job, this part works!")
        break
ZdaR
  • 22,343
  • 7
  • 66
  • 87
  • The `else: continue` is redundant – jonrsharpe May 05 '15 at 13:24
  • Yeah But kind of giving insight to the `continue` statement. – ZdaR May 05 '15 at 13:26
  • Not really a good practice. Avoid `while True` followed by `break` in your code – Yeo May 05 '15 at 13:26
  • Ah, I saw this on another page but I couldn't figure out how to convert it to work for me. Thank you very much for this :) – Obahar May 05 '15 at 13:27
  • But where are you facing problem in implementing this ? This works fine on my machine? – ZdaR May 05 '15 at 13:27
  • 1
    Break is bad in this sense, break should only be used when dealing with case – heinst May 05 '15 at 13:28
  • 1
    @Yeo *"Avoid while True followed by break in your code"* - according to whom? I would say that's absolutely the correct approach, much more Pythonic than e.g. setting a flag or having the `input` in more than once place. – jonrsharpe May 05 '15 at 13:29
  • @heinst what do you mean *"dealing with case"*? – jonrsharpe May 05 '15 at 13:29
  • @jonrsharpe switch/case statements – heinst May 05 '15 at 13:30
  • 1
    @heinst Python doesn't have those. `break` is absolutely for use with loops - see e.g. https://docs.python.org/2/reference/simple_stmts.html#the-break-statement – jonrsharpe May 05 '15 at 13:30
  • There is no need for break in this sense. You could have easily done it the way I did without doing it – heinst May 05 '15 at 13:30
  • And you people are getting me down, Mine answer is working and is OK for a beginner, why so much downvotes ? – ZdaR May 05 '15 at 13:30
  • @jonrsharpe This is more of standard practice of software design pattern, according to my experience. It is like your code having multiple endpoint. This is the same as having `GOTO`. You can terminate from somewhere in the middle of your code. – Yeo May 05 '15 at 13:31
  • 2
    @Yeo it is **not** the same as `GOTO`. `break`ing out of a `for`/`while` loop is perfectly acceptable in Python; see e.g. https://docs.python.org/2/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops – jonrsharpe May 05 '15 at 13:33
  • @jonrsharpe In fact, break is acceptable in most languages, but I believe this is about the flow of the program. I have been debugging and understanding where it is coming from, the termination condition is unknown. It is also harder to analyse the algorithms running time. (at least for me, i try to avoid, until there are too many cases). and a function with too many cases is usually hard to test – Yeo May 05 '15 at 13:38
  • 1
    @Yeo yo're entitled to your opinion, but I don't see how you can claim *"the termination condition is unknown"*. You are throwing the baby out with the bathwater - just because `break` makes some complex loops harder to read doesn't mean you should overcomplicate simple loops, where that doesn't apply, to get rid of it. I think that is what [the style guide](https://www.python.org/dev/peps/pep-0008/) refers to as "*foolish consistency"*. – jonrsharpe May 05 '15 at 13:43
  • @Yeo The question was asked by a beginner and I don't guess he needs such strict and expert guidelines to implement a program as simple and taking input and checking if it matches a pre defined set of values. – ZdaR May 05 '15 at 13:47
  • 1
    @jonrsharpe, anmoluppal thanks i think having too many restriction will only cause barrier for a new user. We all agree on PEP8 convention. :) – Yeo May 05 '15 at 13:50
1

I think you want to check if type1 is in Elements, not if it's equal to it. Elements is a tuple of strings, type1 will be just a string. Those two things will never be equal.

You can test whether it's in using the in keyword as:

while( type1 not in Elements ):
    type1 = raw_input( "Enter a valid type" )
Eric Renouf
  • 13,950
  • 3
  • 45
  • 67
1

You were trying to see if a word equaled a list, this would never be true, you want to see if the word IS IN the list

elements = "Normal","Fire","Water","Grass","Electric","Bug","Flying","Ground","Rock","Posion","Dragon","Dark","Fairy","Psychic","Steel","Fighting","Ice"
type1 = input("Please input a type")
while type1 not in elements:
    type1 = input("Please input a real type")

print("Good Job, this part works!") # But it doesn't get to this point...
heinst
  • 8,520
  • 7
  • 41
  • 77
  • 2
    This approach is bad because you now have `type1 = input("Please input a type")` in **two places**. DRY! `while True` with `break` **is** the Pythonic way to do this, see e.g. http://stackoverflow.com/q/23294658/3001761. Also `Elements` doesn't really *need* to be a list, the tuple was fine. – jonrsharpe May 05 '15 at 13:31
  • Thank you for the corrections and explanations. This was really helpful, thank you – Obahar May 05 '15 at 13:31