Author Topic: Python Easy Examples help  (Read 14225 times)

0 Members and 1 Guest are viewing this topic.

Offline blankanon

  • Serf
  • *
  • Posts: 21
  • Cookies: -1
    • View Profile
Python Easy Examples help
« on: August 12, 2011, 09:51:10 am »
So I was reading some examples and trying to get them to work but nothing happens.



Code: [Select]
>>> # Fibonacci series:
>>> # the sum of two elements defines the next
>>> a, b = 0, 1
>>> while b < 10:
         print b,
         a, b = b, a+b


What do I do to get Python to execute this?
« Last Edit: August 13, 2011, 12:19:45 am by blankanon »

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: Python Fibonacci Series
« Reply #1 on: August 12, 2011, 11:23:05 am »
Mind the indentation. In Python code indentation is compulsory. So after each for/if/while etc. you have to put 4 spaces

Code: [Select]
>>> a, b = (0,  1)
>>> while b < 10:
...     print b
...     a,  b = (b, a+b)
...
1
1
2
3
5
8


I recommend you to read http://www.python.org/dev/peps/pep-0008/
it is a really good paper about code style in Python :)

Offline blankanon

  • Serf
  • *
  • Posts: 21
  • Cookies: -1
    • View Profile
Re: Python Fibonacci Series
« Reply #2 on: August 12, 2011, 08:35:18 pm »
Thanks.  I followed your example and nothing happened as well.  How do I get Python to execute the code?  I went to debugger and that did nothing as well.


Also, can you explain this line a, b = (b, a+b)?
« Last Edit: August 12, 2011, 08:38:27 pm by blankanon »

Offline ande

  • Owner
  • Titan
  • *
  • Posts: 2664
  • Cookies: 256
    • View Profile
Re: Python Fibonacci Series
« Reply #3 on: August 12, 2011, 08:52:44 pm »
Thanks.  I followed your example and nothing happened as well.  How do I get Python to execute the code?

You run python FILENAME/PATH in cmd or terminal.


Also, can you explain this line a, b = (b, a+b)?

I dont really know python that well, but I believe its the same as setting a and b to 1 because a = 0 and b = 1.
if($statement) { unless(!$statement) { // Very sure } }
https://evilzone.org/?hack=true

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: Python Fibonacci Series
« Reply #4 on: August 12, 2011, 10:34:46 pm »
You run python FILENAME/PATH in cmd or terminal.
Not necessarily. 5 lines of code can be executed in an interactive Python shell as well. Don't forget to press Enter again to finish the FOR loop and execute the loop.
If you want, you can run it from a file too. Just save the code in a file and then do: python script.py

I dont really know python that well, but I believe its the same as setting a and b to 1 because a = 0 and b = 1.

You are correct. It is the same thing, although when closed with ( and ) it is counted as a Tuple and as a group of variables. I just find it more clean to do it that way :P

Offline blankanon

  • Serf
  • *
  • Posts: 21
  • Cookies: -1
    • View Profile
Re: Python Fibonacci Series
« Reply #5 on: August 13, 2011, 12:16:37 am »
I had to hit return twice.  Thanks.




Offline blankanon

  • Serf
  • *
  • Posts: 21
  • Cookies: -1
    • View Profile
Re: Python Easy Examples help
« Reply #6 on: August 13, 2011, 01:01:57 am »
Rock, Paper, Scissors game:



Code: [Select]
player1 = raw_input ("Player 1? ") # player 1's move
player2 = raw_input ("Player 2? ") # player 2's move


if (player1 == 'rock' and player2 == 'scissors'):
    print 'Player 1 wins!'


elif (player1 == 'rock' and player2 == 'paper'):
    print 'Player 2 wins!'


elif (player1 == 'scissors' and player2 == 'rock'):
    print 'Player 2 wins!'


elif (player1 == 'scissors' and player2 == 'paper'):
    print 'Player 1 wins!'


elif (player1 == 'paper' and player2 == 'scissors'):
    print 'Player 2 wins!'


elif (player1 == 'paper' and player2 == 'rock'):
    print 'Player 1 wins!'


elif (player1 == player2):
    print 'Tie.'


How can I make this start over if there is a tie? 
How can I make this not care if there are capital letters inputted?


Thanks.
   
« Last Edit: August 13, 2011, 01:02:24 am by blankanon »

Offline xzid

  • Knight
  • **
  • Posts: 329
  • Cookies: 41
    • View Profile
Re: Python Easy Examples help
« Reply #7 on: August 13, 2011, 03:21:20 am »
^
I won't give you code, this is nice learning project. But can give you some advice.

Perhaps shorten input in this style:

(R)ock, (P)aper, (S)cissors

In pseudocode, C-stylish code(not really any code):

Code: [Select]
p1win = "player 1 wins!"
p2win = "player 2 wins!"
retry = true

while(retry)
  {
  retry = false
  p1 = getinput()
  p2 = getinput()

  if(p1 == "R")
    {
    if(p2 == "S") print p1win
    elif(p2 == "P") print p2win
    else retry = true
    }
  else if(p1 == "P")
    {
    # etc.. finish the code
    }
  else
    {

    }
  }

There are better ways to do it, kept it simple. goto would be best, but ppl don't like it.

About capital letters:
  http://docs.python.org/library/stdtypes.html

scroll down to str.upper(), and take a peek at other functions described here.

Offline blankanon

  • Serf
  • *
  • Posts: 21
  • Cookies: -1
    • View Profile
Re: Python Easy Examples help
« Reply #8 on: August 13, 2011, 04:01:42 am »
So here is what I tried and I was told rock isn't defined line 9



Code: [Select]
p1win = "Player 1 wins!"
p2win = "Player 2 wins!"
player1 = raw_input ("Player 1? ") # player 1's move
player2 = raw_input ("Player 2? ") # player 2's move
retry = True


while (retry):
    retry = False
    if player1 == rock:
          if player2 == scissors:
              print p1win
          elif player2 == paper:
              print p2win
          elif player2 == rock:
              print "Tie"
              retry = True
    elif player1 == scissors:
          if player2 == rock:
              print p2win
          elif player2 == paper:
              print p1win
          elif player2 == scissors:
              print "Tie"
              retry = True
    elif player1 == paper:
          if player2 == rock:
              print p1win
          elif player2 == scissors:
              print p2win
          elif player2 == paper:
              print "Tie"
              retry = True


Offline xzid

  • Knight
  • **
  • Posts: 329
  • Cookies: 41
    • View Profile
Re: Python Easy Examples help
« Reply #9 on: August 13, 2011, 04:08:16 am »
don't forget the quotes, they aren't variables only strings.

Offline blankanon

  • Serf
  • *
  • Posts: 21
  • Cookies: -1
    • View Profile
Re: Python Easy Examples help
« Reply #10 on: August 13, 2011, 04:16:49 am »
Thanks put the quotes in, but if there is a tie, the output is an infinite loop of ties.

Offline xzid

  • Knight
  • **
  • Posts: 329
  • Cookies: 41
    • View Profile
Re: Python Easy Examples help
« Reply #11 on: August 13, 2011, 04:20:42 am »
My error, never tested any code.. Input should come from within loop.

Offline blankanon

  • Serf
  • *
  • Posts: 21
  • Cookies: -1
    • View Profile
Re: Python Easy Examples help
« Reply #12 on: August 13, 2011, 04:23:41 am »
So how can I get it to print "Tie" once and then re-start the game?


Offline xzid

  • Knight
  • **
  • Posts: 329
  • Cookies: 41
    • View Profile
Re: Python Easy Examples help
« Reply #13 on: August 13, 2011, 04:27:53 am »
So how can I get it to print "Tie" once and then re-start the game?

Once again didn't test anything.. But in theory just move lines:

player1 = raw_input ("Player 1? ") # player 1's move
player2 = raw_input ("Player 2? ") # player 2's move

right after:

while (retry):
    retry = False

inside your loop, so if loops again will take new input
« Last Edit: August 13, 2011, 04:30:08 am by xzid »

Offline blankanon

  • Serf
  • *
  • Posts: 21
  • Cookies: -1
    • View Profile
Re: Python Easy Examples help
« Reply #14 on: August 13, 2011, 04:44:10 am »
That was it thanks.