EvilZone

Programming and Scripting => Scripting Languages => : blankanon August 12, 2011, 09:51:10 AM

: Python Easy Examples help
: blankanon August 12, 2011, 09:51:10 AM
So I was reading some examples and trying to get them to work but nothing happens.



:
>>> # 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?
: Re: Python Fibonacci Series
: Kulverstukas 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

:
>>> 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/ (http://www.python.org/dev/peps/pep-0008/)
it is a really good paper about code style in Python :)
: Re: Python Fibonacci Series
: blankanon 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)?
: Re: Python Fibonacci Series
: ande 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.
: Re: Python Fibonacci Series
: Kulverstukas 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
: Re: Python Fibonacci Series
: blankanon August 13, 2011, 12:16:37 AM
I had to hit return twice.  Thanks.



: Re: Python Easy Examples help
: blankanon August 13, 2011, 01:01:57 AM
Rock, Paper, Scissors game:



:
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.
   
: Re: Python Easy Examples help
: xzid 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):

:
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 (http://docs.python.org/library/stdtypes.html)

scroll down to str.upper(), and take a peek at other functions described here.
: Re: Python Easy Examples help
: blankanon August 13, 2011, 04:01:42 AM
So here is what I tried and I was told rock isn't defined line 9



:
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

: Re: Python Easy Examples help
: xzid August 13, 2011, 04:08:16 AM
don't forget the quotes, they aren't variables only strings.
: Re: Python Easy Examples help
: blankanon 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.
: Re: Python Easy Examples help
: xzid August 13, 2011, 04:20:42 AM
My error, never tested any code.. Input should come from within loop.
: Re: Python Easy Examples help
: blankanon August 13, 2011, 04:23:41 AM
So how can I get it to print "Tie" once and then re-start the game?

: Re: Python Easy Examples help
: xzid 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
: Re: Python Easy Examples help
: blankanon August 13, 2011, 04:44:10 AM
That was it thanks.
: Re: Python Easy Examples help
: xzid August 13, 2011, 04:51:11 AM
glad could help, for capital problem...  change lines to:

player1 = raw_input ("Player 1? ").lower()

same w/player 2. once again, in theory. Have no python on windows only on my linux.
: Re: Python Easy Examples help
: blankanon August 13, 2011, 05:04:12 AM
I am working on another practice project.  I understand the question, but I am confused by it if that makes sense.


The problem is as stated:


Using a for loop, write a program that prints out the decimal equivalents of 1/n where [tex]n\in\mathbb{Z}[/tex] and [tex]1 \leq n \leq 10[/tex]. 


Does Latex work on this site?  If not, who can I ask about having it installed?


Thanks.
: Re: Python Easy Examples help
: xzid August 13, 2011, 05:07:07 AM
try this:

http://www.codecogs.com/latex/eqneditor.php (http://www.codecogs.com/latex/eqneditor.php)

edit: andy btw...
: Re: Python Easy Examples help
: blankanon August 13, 2011, 05:55:50 AM
I know Latex.  However, it won't work on the site unless it is set up.
: Re: Python Easy Examples help
: xzid August 13, 2011, 06:05:49 AM
yeah exactly, I have no control over it.

But if Z is set of all integers, and you need all integers between 1 and 10... Then create array(range will do) then use for loop and print 1/result.

http://docs.python.org/tutorial/controlflow.html (http://docs.python.org/tutorial/controlflow.html)

4.3 will help.

No more homework help from me.
: Re: Python Easy Examples help
: Kulverstukas August 13, 2011, 12:05:52 PM
You should do some testing and reading and learning more instead of asking these simple questions that you could have solved it by the end of the day... lrn2lrn :|
: Re: Python Easy Examples help
: Kreek August 14, 2011, 11:51:08 AM
You should do some testing and reading and learning more instead of asking these simple questions that you could have solved it by the end of the day... lrn2lrn :|

At least he is programming... But you have a point Kul, like the question with the quotes could be easily been avoided by just trying on your own. But I like his dedication?

@TS
Keep trying, keep making small games like this! When you got problems, just try solve it on your own. And if you got questions after you tried, just post here!
: Re: Python Easy Examples help
: blankanon August 14, 2011, 09:46:28 PM
At least he is programming... But you have a point Kul, like the question with the quotes could be easily been avoided by just trying on your own. But I like his dedication?

@TS
Keep trying, keep making small games like this! When you got problems, just try solve it on your own. And if you got questions after you tried, just post here!


That is what I do.  If I can't figure it out or I am not sure, I ask.

Additionally, I have been watching MIT's OpenCourseWare videos on Introduction to Computer Science and Programming since they use Python.
: Re: Python Easy Examples help
: Kreek August 14, 2011, 11:47:37 PM

That is what I do.  If I can't figure it out or I am not sure, I ask.

Additionally, I have been watching MIT's OpenCourseWare videos on Introduction to Computer Science and Programming since they use Python.

But i'm talking about this:
: blankanon
So here is what I tried and I was told rock isn't defined line 9

That's quite an easy error to fix... You should be able to fix those errors. And if you don't, try to search the internet for your error. And al those things don't work, then post here.


EDIT: Try to make me a OXO-game. ;)
: Re: Python Easy Examples help
: blankanon August 15, 2011, 01:06:18 AM
But i'm talking about this:
That's quite an easy error to fix... You should be able to fix those errors. And if you don't, try to search the internet for your error. And al those things don't work, then post here.


EDIT: Try to make me a OXO-game. ;)


This is only my 3rd day on Python.  I didn't understand what that meant.
: Re: Python Easy Examples help
: Kulverstukas August 15, 2011, 10:42:39 AM
This is only my 3rd day on Python.  I didn't understand what that meant.

Again... you should have googled... http://en.wikipedia.org/wiki/OXO (http://en.wikipedia.org/wiki/OXO)

I don't know but unless you lrn2google, you won't seem to able to learn anything...
: Re: Python Easy Examples help
: blankanon August 15, 2011, 09:13:12 PM
Again... you should have googled... http://en.wikipedia.org/wiki/OXO (http://en.wikipedia.org/wiki/OXO)

I don't know but unless you lrn2google, you won't seem to able to learn anything...


I never said I didn't understand what OXO meant.  I was referring to Kreek comments minus the edit piece. 

That should have been easily inferred by the past tense (didn't).  If I would have said I don't, then it would have been implying the current post.

Even though you are an admin, don't be an ass just to be one.
: Re: Python Easy Examples help
: Kulverstukas August 15, 2011, 09:41:40 PM

I never said I didn't understand what OXO meant.  I was referring to Kreek comments minus the edit piece. 

That should have been easily inferred by the past tense (didn't).  If I would have said I don't, then it would have been implying the current post.

Even though you are an admin, don't be an ass just to be one.
I am not trying nor I am an ass - I am being fairly good.
Since this is a forum, I can't hear your voice tone, so admit it that it was your fault that you did not refer to the OXO game asking you did not understand, yet you did quote that line, therefore I assumed you were referring to that line seeing as the referring sentence is after the dot after the sentence that explains your lack of skill in Python, which was stated in Kreek's post.
: Re: Python Easy Examples help
: blankanon August 16, 2011, 03:57:51 AM
I am not trying nor I am an ass - I am being fairly good.
Since this is a forum, I can't hear your voice tone, so admit it that it was your fault that you did not refer to the OXO game asking you did not understand, yet you did quote that line, therefore I assumed you were referring to that line seeing as the referring sentence is after the dot after the sentence that explains your lack of skill in Python, which was stated in Kreek's post.


You made the cardinal sin--you assumed.  I was fairly clear in the structure of my sentence that I was speaking of a something in the past and not present (i.e. not the current thread post).


However, I need to focus on programming not the semantic interpretations of the word "didn't".



: Re: Python Easy Examples help
: xzid August 16, 2011, 06:09:02 AM
Just curious which sin does assuming fall under, pride?

Additionally, I have been watching MIT's OpenCourseWare videos on Introduction to Computer Science and Programming since they use Python.

Learning programming through videos seems like a bad idea to me. Just read a regular tutorial. Once you got a hang of python, then you could research specific things that interest you.

... Also chill with the grammar, both of you.
: Re: Python Easy Examples help
: blankanon August 17, 2011, 01:25:24 AM


Learning programming through videos seems like a bad idea to me. Just read a regular tutorial. Once you got a hang of python, then you could research specific things that interest you.




The videos are great since they are actually lecture videos. 


:
# Calculates the FV of an investment
# Compoud interest

def main():
    print("This program calculates FV of an investment")


    principal = eval(input("Enter the initial investment: "))
    apr = eval(input("Enter the apr: "))
    years = eval(input("Enter years of investment: "))


    for i in range(years):
        principal = principal * (1 + apr)


    print("The value in", years, "is:", principal)


I keep getting:

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    main()
  File "C:/Python27/Projects/FV calculator.py", line 7, in main
    principal = eval(input("Enter the initial investment: "))
TypeError: eval() arg 1 must be a string or code object


This happens whenever I enter a number in.
: Re: Python Easy Examples help
: Stackprotector August 17, 2011, 01:48:11 AM
why are u using eval?  and why aren't u using raw_input instead of input?
: Re: Python Easy Examples help
: blankanon August 17, 2011, 02:14:23 AM
The book I am using used that for this program.
: Re: Python Easy Examples help
: Stackprotector August 17, 2011, 02:34:56 AM
:
# Calculates the FV of an investment
# Compoud interest

def main():
    print("This program calculates FV of an investment")


    principal = raw_input("Enter the initial investment: ")
    apr = raw_input("Enter the apr: ")
    years = raw_input("Enter years of investment: ")


    for i in range(years):
        principal = principal * (1 + apr)


    print("The value in", years, "is:", principal)
Use that ;)
: Re: Python Easy Examples help
: xzid August 17, 2011, 02:41:55 AM
Your code makes absolutely no sense. If you were able to get input in your R/P/S game, why'd you change it? Do you have any idea what input() & eval() do? Do you know what this means:

TypeError: eval() arg 1 must be a string or code object

and why python is telling you this. If not, find out. This is where learning starts... Not asking others to fix your code for you.

And yeah, your video lecture is a horrible idea.. Your time, not mine.

: Re: Python Easy Examples help
: blankanon August 17, 2011, 03:22:21 AM
Your code makes absolutely no sense. If you were able to get input in your R/P/S game, why'd you change it? Do you have any idea what input() & eval() do? Do you know what this means:

TypeError: eval() arg 1 must be a string or code object

and why python is telling you this. If not, find out. This is where learning starts... Not asking others to fix your code for you.

And yeah, your video lecture is a horrible idea.. Your time, not mine.


First of all, I didn't ask anyone to fix my code.  I guess reading is optional on this forum.  I wanted to know why that error was occurring.  I was putting in numbers but it wasn't working. Furthermore, this problem I obtained from a book not the lectures.  If you read what I wrote, I stated that.


If you want to comment on my post, read first.  Type second.
: Re: Python Easy Examples help
: xzid August 17, 2011, 03:47:28 AM
First of all, I didn't ask anyone to fix my code.

Then stop posting skiddy code here, google dipshit. You know when learning to program, I've never had to ask anyone on a forum for help.

I wanted to know why that error was occurring.  I was putting in numbers but it wasn't working.

Cus you're a moron.

I guess reading is optional on this forum.

yeah seems to be problem with you too, never said it was from lectures.. I was actually replying to this:
 
 
The videos are great since they are actually lecture videos.

ouch btw, -1 really hurts :'(
: Re: Python Easy Examples help
: blankanon August 17, 2011, 03:55:06 AM
Then stop posting skiddy code here, google dipshit. You know when learning to program, I've never had to ask anyone on a forum for help.

Cus you're a moron.

yeah seems to be problem with you too, never said it was from lectures.. I was actually replying to this:
 
 
ouch btw, -1 really hurts :'(


The feeble minded always resort to insults. 
: Re: Python Easy Examples help
: xzid August 17, 2011, 03:55:38 AM

The feeble minded always resort to insults.

and a pussy never fights back.
: Re: Python Easy Examples help
: Stackprotector August 18, 2011, 10:50:20 AM
Blankanon,    I am asking serveral times why are u using evals.
"Because the book tells me to"
NO why are u using evals?, google it, google what an eval is,  and come back later.
: Re: Python Easy Examples help
: flowjob February 05, 2012, 10:12:46 PM
Ok,firstly I guess the reason why the book is using 'input()' is because the book is for Python 3.x
In Python 3.x 'input' is the same like 'raw_input' (<-- for strings) in 2.x, a thing like 'input' in 2.x for int does,as I know, not exist anymore.
Secondly 'eval' turns a string into an calculation.

The error is because you are using Python 2.x so 'input' will be an int and 'eval' needs a string.
: Re: Python Easy Examples help
: flowjob February 05, 2012, 10:22:46 PM
And here the correct script:

: (python)
#Calculates the FV of an investment
#Compoud interest

def main():
    print 'This program calculates FV of an investment'

    principal = raw_input('Enter the initial investment: ')
    apr = raw_input('Enter the apr: ')
    years = input('Enter years of investment: ')

    for i in range(years):
        principal = eval(principal*(1+apr))
    print 'The value in ' + str(years) + ' is: ' + str(principal)
: Re: Python Easy Examples help
: Stackprotector February 05, 2012, 11:03:27 PM
And here the correct script:

#Calculates the FV of an investment
#Compoud interest

def main():
    print 'This program calculates FV of an investment'

    principal = raw_input('Enter the initial investment: ')
    apr = raw_input('Enter the apr: ')
    years = input('Enter years of investment: ')

    for i in range(years):
        principal = eval(principal*(1+apr))
    print 'The value in ' + str(years) + ' is: ' + str(principal)

This post of you is a real contribution, but next time try to evade old topics :).

Thanks
: Re: Python Easy Examples help
: flowjob February 06, 2012, 05:42:53 PM
I knew this was an old topic,but I thougt maybe someone other has the same problems with eval/input/raw_input and may look in this topic.