EvilZone
Programming and Scripting => Scripting Languages => : 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?
-
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 :)
-
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)?
-
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.
-
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
-
I had to hit return twice. Thanks.
-
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.
-
^
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.
-
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
-
don't forget the quotes, they aren't variables only strings.
-
Thanks put the quotes in, but if there is a tie, the output is an infinite loop of ties.
-
My error, never tested any code.. Input should come from within loop.
-
So how can I get it to print "Tie" once and then re-start the game?
-
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
-
That was it thanks.
-
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.
-
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.
-
try this:
http://www.codecogs.com/latex/eqneditor.php (http://www.codecogs.com/latex/eqneditor.php)
edit: andy btw...
-
I know Latex. However, it won't work on the site unless it is set up.
-
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.
-
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 :|
-
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!
-
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.
-
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:
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. ;)
-
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.
-
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...
-
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.
-
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.
-
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".
-
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.
-
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.
-
why are u using eval? and why aren't u using raw_input instead of input?
-
The book I am using used that for this program.
-
# 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 ;)
-
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.
-
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.
-
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 :'(
-
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.
-
The feeble minded always resort to insults.
and a pussy never fights back.
-
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.
-
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.
-
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)
-
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
-
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.