It is currently Wed Jun 19, 2013 5:05 pm Advanced search

I can't get the tutorial to work (Using Python)

New members are welcomed here to introduce themselves, meet your opponents here.
This is a place for website/account issues too.

I can't get the tutorial to work (Using Python)

Postby MiltonBradley » Tue Nov 15, 2011 5:40 am

Can I get some help getting the "getting started in python" code to work

from
http://aichallenge.org/getting_started_with_python.php

Code: Select all
import argparse
from math import sqrt, floor

def primes(n):
    sieve = {x: True for x in range(2, n)}
    for num in range(2, floor(sqrt(n))):
        for multiple in range(num ** 2, n, num):
            sieve[multiple] = False
    return [x for x, is_prime in sieve.items() if is_prime]

def main():
    parser = argparse.ArgumentParser(description='List prime numbers.')
    parser.add_argument('--max', type=int, default=100,
                        help='highest prime number to generate')
    args = parser.parse_args()

    print(' '.join(map(str, primes(args.max + 1))))

if __name__ == '__main__':
    main()


Error:
Code: Select all
Traceback (most recent call last):
  File "C:/Users/Paul/Desktop/playgame.py", line 20, in <module>
    main()
  File "C:/Users/Paul/Desktop/playgame.py", line 17, in main
    print(' '.join(map(str, primes(args.max + 1))))
  File "C:/Users/Paul/Desktop/playgame.py", line 6, in primes
    for num in range(2, floor(sqrt(n))):
TypeError: range() integer end argument expected, got float.
MiltonBradley
Lieutenant
 
Posts: 19
Joined: Sat Nov 12, 2011 7:18 am

Re: I can't get the tutorial to work (Using Python)

Postby mac » Tue Nov 15, 2011 7:05 am

The stack really tells it all already: you have to provide an integer as final point of your range, not a float: see if this dump helps you:

Code: Select all
>>> range(1, 3)
[1, 2]
>>> range(1, 3.0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: range() integer end argument expected, got float.
>>> range(1, int(3.0))
[1, 2]
mac
Brigadier-General
 
Posts: 151
Joined: Mon Oct 31, 2011 6:39 am

Re: I can't get the tutorial to work (Using Python)

Postby MiltonBradley » Tue Nov 15, 2011 5:06 pm

So the stack means the error message?

I don't know that I provided a float or an integer, and I am not sure how to provide that integer.

I typed "python prime.py --max 1337" into the command line just as the instructions said. Is the 1337 the integer you are referring to?

I tried "python prime.py --max 10" and got the same error.
MiltonBradley
Lieutenant
 
Posts: 19
Joined: Sat Nov 12, 2011 7:18 am

Re: I can't get the tutorial to work (Using Python)

Postby mac » Wed Nov 16, 2011 7:15 am

Milton, from the kind of question you are making I would assume you are a very beginner to programming in general and to python in particular.

This forum might not be the best place to seek help at that level (although nothing prevents you to try, of course). May I suggest that you subscribe to the tutor mailing list on the python site? Folks there are awesome and very responsive!

http://mail.python.org/mailman/listinfo/tutor
mac
Brigadier-General
 
Posts: 151
Joined: Mon Oct 31, 2011 6:39 am

Re: I can't get the tutorial to work (Using Python)

Postby MiltonBradley » Wed Nov 16, 2011 4:11 pm

mac wrote:Milton, from the kind of question you are making I would assume you are a very beginner to programming in general and to python in particular.

This forum might not be the best place to seek help at that level (although nothing prevents you to try, of course). May I suggest that you subscribe to the tutor mailing list on the python site? Folks there are awesome and very responsive!

http://mail.python.org/mailman/listinfo/tutor


The problem was it was python 3 trying to run python 2 code. It's working fine now. The tutorial says to use either python 2 or python 3, but it only runs with 2.
MiltonBradley
Lieutenant
 
Posts: 19
Joined: Sat Nov 12, 2011 7:18 am

Re: I can't get the tutorial to work (Using Python)

Postby mac » Wed Nov 16, 2011 4:30 pm

MiltonBradley wrote:
mac wrote:The tutorial says to use either python 2 or python 3, but it only runs with 2.


They are two different starter packages. Both of them work (in combination with the right version of python, of course).
mac
Brigadier-General
 
Posts: 151
Joined: Mon Oct 31, 2011 6:39 am

Re: I can't get the tutorial to work (Using Python)

Postby MiltonBradley » Wed Nov 16, 2011 7:13 pm

mac wrote:
MiltonBradley wrote:
mac wrote:The tutorial says to use either python 2 or python 3, but it only runs with 2.


They are two different starter packages. Both of them work (in combination with the right version of python, of course).


Are you sure you don't have to have python 2 regardless of which language you use?
MiltonBradley
Lieutenant
 
Posts: 19
Joined: Sat Nov 12, 2011 7:18 am

Re: I can't get the tutorial to work (Using Python)

Postby mac » Wed Nov 16, 2011 7:22 pm

MiltonBradley wrote:Are you sure you don't have to have python 2 regardless of which language you use?


AFAIK you need to have python 2 to run the game engine.
Additionally you need to have python 3 if you are developing your bot in python 3.
mac
Brigadier-General
 
Posts: 151
Joined: Mon Oct 31, 2011 6:39 am

Re: I can't get the tutorial to work (Using Python)

Postby MiltonBradley » Wed Nov 16, 2011 9:06 pm

mac wrote:
MiltonBradley wrote:Are you sure you don't have to have python 2 regardless of which language you use?


AFAIK you need to have python 2 to run the game engine.
Additionally you need to have python 3 if you are developing your bot in python 3.


ok gotcha. That's what I was now thinking. Thanks for the clarification
MiltonBradley
Lieutenant
 
Posts: 19
Joined: Sat Nov 12, 2011 7:18 am


Return to Introductions

Who is online

Users browsing this forum: No registered users and 1 guest

cron