Re: [Tutor] Simple guessing game - need help with the math

2014-08-15 Thread Sibylle Koczian

Am 13.08.2014 01:25, schrieb Greg Markham:

while answer == "h" or "l" or "c":
 print ("My guess is: ", guess, "\n")
 answer = input("Is it (H)igher? (L)ower? Or am I (C)orrect? ")
 answer = answer.lower()
 if answer == "h":
 guess = round(int(guess + (change/2)))
 change = change/2
 tries += 1
 elif answer == "l":
 guess = round(int(guess - (guess/2)))
 tries += 1
 elif answer == "c":
 print ("/n/nYay!  I win.  Shall we play again?\n\n")
 os.system('cls' if os.name  == 'nt' else 'clear')
 else:
 print ("Invalid response.  Please try again.\n")



Something else is wrong, besides the math: this is an infinite loop, 
because (answer == "h" or "l" or "c") always evaluates to True.


And with the condition you probably wanted:

while answer == "h" or answer == "l" or answer == "c":

the loop wouldn't even start because you set answer = "" in the beginning.





___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Simple guessing game - need help with the math

2014-08-15 Thread Derek Jenkins
I am a newbie (please correct me if I am wrong), but I happen to think
that it would be best to suggest that your input be in the same case
as that of your variables. In other words, it appears that you are
suggesting the user make an input choice of H, L, or C while the
program appears to want to handle the variables h, l, or c.

Clearly my statements do not directly relate to math, but I do not see
how your program would be able to handle the input of H, L, or C.

On Fri, Aug 15, 2014 at 12:49 PM, Derek Jenkins  wrote:
> I am a newbie (please correct me if I am wrong), but I happen to think
> that it would be best to suggest that your input be in the same case
> as that of your variables. In other words, it appears that you are
> suggesting the user make an input choice of H, L, or C while the
> program appears to want to handle the variables h, l, or c.
>
> Clearly my statements do not directly relate to math, but I do not see
> how your program would be able to handle the input of H, L, or C.
>
> On Fri, Aug 15, 2014 at 3:37 AM, Sibylle Koczian  
> wrote:
>> Am 13.08.2014 01:25, schrieb Greg Markham:
>>>
>>> while answer == "h" or "l" or "c":
>>>  print ("My guess is: ", guess, "\n")
>>>  answer = input("Is it (H)igher? (L)ower? Or am I (C)orrect? ")
>>>  answer = answer.lower()
>>>  if answer == "h":
>>>  guess = round(int(guess + (change/2)))
>>>  change = change/2
>>>  tries += 1
>>>  elif answer == "l":
>>>  guess = round(int(guess - (guess/2)))
>>>  tries += 1
>>>  elif answer == "c":
>>>  print ("/n/nYay!  I win.  Shall we play again?\n\n")
>>>  os.system('cls' if os.name  == 'nt' else 'clear')
>>>
>>>  else:
>>>  print ("Invalid response.  Please try again.\n")
>>>
>>
>> Something else is wrong, besides the math: this is an infinite loop, because
>> (answer == "h" or "l" or "c") always evaluates to True.
>>
>> And with the condition you probably wanted:
>>
>> while answer == "h" or answer == "l" or answer == "c":
>>
>> the loop wouldn't even start because you set answer = "" in the beginning.
>>
>>
>>
>>
>>
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Simple guessing game - need help with the math

2014-08-15 Thread Alan Gauld

On 15/08/14 17:49, Derek Jenkins wrote:

I am a newbie (please correct me if I am wrong), but I happen to think
that it would be best to suggest that your input be in the same case
as that of your variables. In other words, it appears that you are
suggesting the user make an input choice of H, L, or C while the
program appears to want to handle the variables h, l, or c.



  answer = input("Is it (H)igher? (L)ower? Or am I (C)orrect? ")
  answer = answer.lower()


You missed the lower() call in the line above.
So that aspect at least is covered.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Building Starships -- object of type 'int' has no len()

2014-08-15 Thread Terry--gmail

Python 3.3

This has something to do with the nature of FOR statements and IF 
statements, and I am sure it must be a simple mistake...but I seem to be 
stumped.


I am writing a starship encounter program as my first real python 
programwhere the user gets a random amount of credits to design his 
own ship and the computer also gets a random number of credits to spend 
and designs it's own ship, and then they go into combat against each other.


This is part of the design phase for the user. The user has picked one 
category of the catalog, such as:


Hulls, Drives, Shields, Weapons, Personnel, etc

catalog and catalog2 are lists of lists:  [[],[],[]]

OK. So, my program has scanned the master catalog for a particular 
chosen category and has built catalog2 containing all the parts of that 
one category and their associated differences (rows), and now I want to 
print it out as a menu for the user to pick what part to add of this 
particular category to his ship.


But first, so that I can pretty print the menu items and their 
associated capabilities to the screen in nice uniform columns, I need to 
know the maximum size of what is going to be printed in each column on 
the screen in advance, so I do the below learning scan through catagory2 
and I build the simple list 'lens' to contain the max size of each column.


As I run the program, this works perfectly for every NEW category the 
user picks, and the rows of associated data are printed just like I 
want...*UNTIL the user chooses to buy a second part from a category 
he has already visited*, and at that point things error out. Below is 
the problem code and the error:



lens = []

# pre-format the list called lens for maximum number of columns 
contained in catalog2


lens = [0] * len(catalog2[0])

# map the largest sizes of each column into list 'lens'

col, line_number = 0, 0

for line_number in range(len(catalog2)):

for col in range(len(catalog2[line_number])):

*if lens[col] < len(catalog2[line_number][col]):*

lens[col] = len(catalog2[line_number][col])


Traceback (most recent call last):
  File "encounter.py", line 379, in 
myship = designShip(credits, myship)
  File "encounter.py", line 354, in designShip
hull, myship, credits, section = nicePrint(hull, credits, myship, 
catalog, section)

  File "encounter.py", line 199, in nicePrint
if lens[col] < len(catalog2[line_number][col]):
TypeError: *object of type 'int' has no len()*

I don't get it. This code does the same job, again and again 
successfully as I proceed to pick new categories. Why would it care if I 
want to go over the same ground more than once??? It seems to me that 
the FOR STATEMENT automatically zeros the counters 'col' and 
line_number' when it starts counting, but why would it suddenly choke 
because of going over old territory???


In trying to debug i have checked catalog2 and it's data is present and 
intact. I added  the line 'col, line_number = 0, 0' in the sequence of 
events although I don't see why it would be needed, and evidently it 
isn't needed because the problem persists. What's left to check?


I really want to buy more guns and bombs and armor! But the program 
seems to be on the side of gun control!!!

:)
Thanks for your thoughts and suggestions!


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Building Starships -- object of type 'int' has no len()

2014-08-15 Thread Terry--gmail

Thanks for your response JL.

I added the following Exception to the code snippet:

for line_number in range(len(catalog2)):

for col in range(len(catalog2[line_number])):

try:

if lens[col] < len(catalog2[line_number][col]):

lens[col] = len(catalog2[line_number][col])

except TypeError:

print(catalog2)

print("col #: ", col)

print("line_number", line_number)

print("catalog2 col content: ", catalog2[line_number][col])

print("catalog2 col content TYPE: ", type(catalog2[line_number][col]))

exit()


Below is the exception printout. I ordered the catalog2 printout portion 
to be more readable:


[['Drives', 'Type', 'Price', 'Max Speed', 'Energy Drain', 'Rq-Crew', '', 
'', ''],
 ['Drives', 'Solar Sail', 3, 1, 'None', 2, '', '', ''], 
<

Re: [Tutor] Building Starships -- object of type 'int' has no len()

2014-08-15 Thread Marc Tompkins
On Fri, Aug 15, 2014 at 10:46 AM, Terry--gmail  wrote:

(By the way - your indentation got flattened - cue the inevitable
chorus of "DON'T POST TO THIS LIST IN HTML" - so this is my best-guess
reconstruction.)
> lens = []
> # pre-format the list called lens for maximum number of columns contained in
> catalog2
> lens = [0] * len(catalog2[0])
> # map the largest sizes of each column into list 'lens'
> col, line_number = 0, 0
> for line_number in range(len(catalog2)):
> for col in range(len(catalog2[line_number])):
> if lens[col] < len(catalog2[line_number][col]):
> lens[col] = len(catalog2[line_number][col])

There are two separate issues here - the syntactic mistake that's
giving you the error, and the non-Pythonic nature of "for col in
range()" - let's deal with the first one.

catalog2 is a list containing lines; line_number is a list containing
integers.  catalog2[line_number][col] is an integer; len(int) gives
you a TypeError, as you've seen.
I don't entirely understand what you're trying to do, so I can't tell
you whether you want
> if lens[col] < len(catalog2[line_number]):
or
> if lens[col] < catalog2[line_number][col]:
but it's probably one or the other.


Now, for the second issue - to take the length of a list and use that
as the limit for a one-by-one crawl through the list is not the Python
way.  Rather than:
> for line_number in range(len(catalog2)):
use
> for line_number, item in enumerate(catalog2):

https://docs.python.org/3/tutorial/datastructures.html#looping-techniques
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Building Starships -- object of type 'int' has no len()

2014-08-15 Thread leam hall
On a totally side note, I'm watching this because I want to do my own
starship stuff. Long time Traveller player.

-- 
Mind on a Mission
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Building Starships -- object of type 'int' has no len()

2014-08-15 Thread Dave Angel
Terry--gmail  Wrote in message:
>
> 
Please don't post here in html mail. Tell your email program to
 use text. Your program fragment displayed here as a mess, which
 is one of many problems with html. 

Please use reply-list (or whatever your email supports,  like
 reply-all and remove the extra recipients) when adding a message
 to a thread.  By starting a new thread,  you've robbed us of
 context. 

Now to your code, which I can't see because of the html message. 

Don't use bare except,  unless that's throwaway code designed only
 to uncover the type mismatch. 

I couldn't see enough of your code to figure where you're messing
 up a type.  But you should probably write a validation function
 to make sure all the fields are of expected types, and call it
 before and after the update to narrow down the culprit.
 

-- 
DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Building Starships -- object of type 'int' has no len()

2014-08-15 Thread Joseph Lee

Hi,
My thoughts are in the message:



- Original Message -
From: Terry--gmail Subject: [Tutor] Building Starships -- object of type 'int' has 
no len()


Python 3.3

This has something to do with the nature of FOR statements and IF
statements, and I am sure it must be a simple mistake...but I 
seem to be

stumped.

I am writing a starship encounter program as my first real python
programwhere the user gets a random amount of credits to 
design his
own ship and the computer also gets a random number of credits to 
spend
and designs it's own ship, and then they go into combat against 
each other.


This is part of the design phase for the user.  The user has 
picked one

category of the catalog, such as:

Hulls, Drives, Shields, Weapons, Personnel, etc

catalog and catalog2 are lists of lists:  [[],[],[]]

OK.  So, my program has scanned the master catalog for a 
particular
chosen category and has built catalog2 containing all the parts 
of that
one category and their associated differences (rows), and now I 
want to
print it out as a menu for the user to pick what part to add of 
this

particular category to his ship.

But first, so that I can pretty print the menu items and their
associated capabilities to the screen in nice uniform columns, I 
need to
know the maximum size of what is going to be printed in each 
column on
the screen in advance, so I do the below learning scan through 
catagory2
and I build the simple list 'lens' to contain the max size of 
each column.


As I run the program, this works perfectly for every NEW category 
the
user picks, and the rows of associated data are printed just like 
I
want...*UNTIL the user chooses to buy a second part from a 
category
he has already visited*, and at that point things error out.  
Below is

the problem code and the error:


lens = []

# pre-format the list called lens for maximum number of columns
contained in catalog2

lens = [0] * len(catalog2[0])

# map the largest sizes of each column into list 'lens'

col, line_number = 0, 0

for line_number in range(len(catalog2)):

for col in range(len(catalog2[line_number])):

*if lens[col] < len(catalog2[line_number][col]):*

lens[col] = len(catalog2[line_number][col])


Traceback (most recent call last):
  File "encounter.py", line 379, in hull, myship, credits, section = nicePrint(hull, credits, 
myship,

catalog, section)
  File "encounter.py", line 199, in nicePrint
if lens[col] < len(catalog2[line_number][col]):


JL: Hmmm, what is the type of the item stored at that location? 
Based on the surrounding code, what you're telling me is that you 
wish to know the length of that particular line/column location, 
and if it is not a sequence (list, string, etc.), Python will 
throw that error.  So it appears to me that you're attempting to 
use something that the type of the item doesn't support.



TypeError: *object of type 'int' has no len()*

I don't get it.  This code does the same job, again and again
successfully as I proceed to pick new categories.  Why would it 
care if I
want to go over the same ground more than once??? It seems to me 
that

the FOR STATEMENT automatically zeros the counters 'col' and
line_number' when it starts counting, but why would it suddenly 
choke

because of going over old territory???

In trying to debug i have checked catalog2 and it's data is 
present and
intact.  I added  the line 'col, line_number = 0, 0' in the 
sequence of
events although I don't see why it would be needed, and evidently 
it

isn't needed because the problem persists.  What's left to check?

I really want to buy more guns and bombs and armor! But the 
program

seems to be on the side of gun control!!!
:)
Thanks for your thoughts and suggestions!





Cheers,
Joseph
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Building Starships -- object of type 'int' has no len()

2014-08-15 Thread Joseph Lee

Hi Terry (if that's your name),
Start slow - don't think ahead too much.  It is a good thing that 
you know the end product, but sometimes when building a real-life 
system, you might want to work with one aspect of the program at 
a time (input, calculations, print, etc.) to make sure it works 
as advertised before moving onto other things.  Also, I put print 
statements that prints "before and after" picture of a variable 
to make sure that I and the computer are in agreement as to what 
is what.


Good luck.
Cheers,
Joseph (UC Riverside)
- Original Message -
From: Terry--gmail Subject: [Tutor] Building Starships -- object of type 'int' has 
no len()


Thanks for your response JL.

I added the following Exception to the code snippet:

for line_number in range(len(catalog2)):

for col in range(len(catalog2[line_number])):

try:

if lens[col] < len(catalog2[line_number][col]):

lens[col] = len(catalog2[line_number][col])

except TypeError:

print(catalog2)

print("col #: ", col)

print("line_number", line_number)

print("catalog2 col content: ", catalog2[line_number][col])

print("catalog2 col content TYPE: ", 
type(catalog2[line_number][col]))


exit()


Below is the exception printout.  I ordered the catalog2 printout 
portion

to be more readable:

[['Drives', 'Type', 'Price', 'Max Speed', 'Energy Drain', 
'Rq-Crew', '',

'', ''],
 ['Drives', 'Solar Sail', 3, 1, 'None', 2, '', '', ''],
<