Help with TypeError: Can't convert 'list' object to str implicitly
Hello,
Background. My 11 y/o son and I have taken on the task to learn python and
work our way through the http://inventwithpython.com/chapters/ book.
- We are currently on Chapter 9 and trying to modify the hangman program.
- the first challenge was to modify the word list into a dictionary. So we
changed our words variable into a dictionary
-- I believe we did this correctly.
- But we somehow broke the game. All of the words are only two letters and
the program crashes on exit with the following error.
Traceback (most recent call last):
File "/media/.../Python/hangman.py", line 155, in
print('You have run out of guesses! \n After ' + str(len(missedLetters)) +
' missed guesses and ' + str(len(correctLetters)) + ' correct guesses, the word
was "' + secretWord + '"')
TypeError: Can't convert 'list' object to str implicitly
-I can't see why this wouldn't work. By definition isn't this the cast:
1) len(correctLetters) //returns the lengths of the variable as an int
2) str(len(correctLetters)) // converts the integer into a string.
Applicable code is here:
# Check if player has guessed too many times and lost
if len(missedLetters) == len(HANGMANPICS) - 1:
displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)
print('You have run out of guesses! \n After ' +
str(len(missedLetters)) + ' missed guesses and ' + str(len(correctLetters)) + '
correct guesses, the word was "' + secretWord + '"')
gameIsDone = True
Any help to get us past this error message is most appreciated.
Thanks in advance,
Dave
--
https://mail.python.org/mailman/listinfo/python-list
[Solved]Re: Help with TypeError: Can't convert 'list' object to str implicitly
On Wednesday, February 5, 2014 7:21:29 PM UTC-7, dave em wrote:
> Hello,
>
>
>
> Background. My 11 y/o son and I have taken on the task to learn python and
> work our way through the http://inventwithpython.com/chapters/ book.
>
> - We are currently on Chapter 9 and trying to modify the hangman program.
>
>
>
> - the first challenge was to modify the word list into a dictionary. So we
> changed our words variable into a dictionary
>
> -- I believe we did this correctly.
>
>
>
> - But we somehow broke the game. All of the words are only two letters and
> the program crashes on exit with the following error.
>
>
>
> Traceback (most recent call last):
>
> File "/media/.../Python/hangman.py", line 155, in
>
> print('You have run out of guesses! \n After ' + str(len(missedLetters))
> + ' missed guesses and ' + str(len(correctLetters)) + ' correct guesses, the
> word was "' + secretWord + '"')
>
> TypeError: Can't convert 'list' object to str implicitly
>
>
>
> -I can't see why this wouldn't work. By definition isn't this the cast:
>
>
>
> 1) len(correctLetters) //returns the lengths of the variable as an int
>
> 2) str(len(correctLetters)) // converts the integer into a string.
>
>
>
> Applicable code is here:
>
> # Check if player has guessed too many times and lost
>
> if len(missedLetters) == len(HANGMANPICS) - 1:
>
> displayBoard(HANGMANPICS, missedLetters, correctLetters,
> secretWord)
>
> print('You have run out of guesses! \n After ' +
> str(len(missedLetters)) + ' missed guesses and ' + str(len(correctLetters)) +
> ' correct guesses, the word was "' + secretWord + '"')
>
> gameIsDone = True
>
>
>
> Any help to get us past this error message is most appreciated.
>
>
>
> Thanks in advance,
>
> Dave
Fixed the error and am now onto the next issue.
Solution was to return a list (I think) and then break out the components of
the list and put in the variable. Here is how we did it:
secretWord = getRandomWord(words)
print('The secretWord is ' + str(secretWord[0]))
print('The secretKey is ' + str(secretWord[1]))
#Change secretWord from a list to a str
secretWord = secretWord[1]
def getRandomWord(wordDict):
# This function returns a random string from the passed dictionary of lists
of strings, and the key also.
# First, randomly select a key from the dictionary:
wordKey = random.choice(list(wordDict.keys()))
print('The wordKey is ' + wordKey)
# Second, randomly select a word from the key's list in the dictionary:
wordIndex = random.randint(0, len(wordDict[wordKey]) - 1)
print('The wordIndex is ' + str(wordIndex))
print('The word is ' + wordDict[wordKey][wordIndex])
return [wordDict[wordKey][wordIndex], wordKey]
Cheers,
Dave
--
https://mail.python.org/mailman/listinfo/python-list
Help with an 8th grade science project
Hello,
I am the adult advisor (aka father) to an 8th grader who is doing a science
project that will compare / benchmark CPU performance between an AMD 64 Phenom
II running Ubuntu 14.04 and a Raspberry Pi 700MHz ARM.
Basic procedure:
- Run a Python script on both computers that that stresses the CPU and measure
-- Time to complete the calculation
-- Max CPU during the calculation
-- We have chosen to do factorials and compare performance by running
calculations by order of magnitude. Our hypothesis is that we will begin to
see a wider performance gap between the two computers as the factorials
increase in order of magnitude.
Status:
- We have a working program. Pseudo code follows:
import linux_metrics
from linux_metrics import cpu_stat
import time
print 'Welcome to the stress test'
number = raw_input("Enter the number to compute the factorial:")
## function to calculate CPU usage percentage
def CPU_Percent():
cpu_pcts = cpu_stat.cpu_percents(.25)
print 'cpu utilization: %.2f%%' % (100 - cpu_pcts['idle'])
write cpu utilization to a csv file with g.write
## function to compute factorial of a given number
def factorial(n):
num = 1
while n >= 1:
num = num * n
CPU_Percent() This is the function call irt Q 1 below
n = n - 1
return num
# Main program
Record start time by using time.time()
Call function to compute the factorial.
Record finish time by using time.time()
write time to compute to a file f.write(totalEndTime - totalStartTime)
print ("Execution time = ", totalEndTime - totalStartTime)
Questions:
1. In the factorial() function we call the CPU_Percent() function and write
the CPU utilization value to a file.
- Is this a correct value or will the CPU utilization below lower because the
factorial() function made its calculation and is now just checking the CPU
utilization?
- If we are not getting the true max CPU utilization, can someone offer a
design change to accomplish this?
2. What unit does time.time() use? An example for calculating the factorial
of 10 is our program gives:
Execution time = ', 1.5703258514404297 I presume this is telling us it took
1.57 seconds to complete the calculation?
Thanks in advance for any help / suggestions.
Best regards,
Dave
--
https://mail.python.org/mailman/listinfo/python-list
Explanation of list reference
Hello, Background: My twelve y/o son and I are still working our way through Invent Your Own Computer Games with Python, 2nd Edition. (We finished the Khan Academy Javascript Tutorials is the extent of our experience) He is asking a question I am having trouble answering which is how a variable containing a value differs from a variable containing a list or more specifically a list reference. I tried the to explain as best I can remember is that a variable is assigned to a specific memory location with a value inside of it. Therefore, the variable is kind of self contained and if you change the variable, you change the value in that specific memory location. However, when a variable contains a list reference, the memory location of the variable points to a separate memory location that stores the list. It is also possible to have multiple variable that point to the memory location of the list reference. And all of those variable can act upon the list reference. Question: Is my explanation correct? If not please set me straight :) And does anyone have an easier to digest explanation? Thanks in advance, Dave -- https://mail.python.org/mailman/listinfo/python-list
Re: Explanation of list reference
On Friday, February 14, 2014 11:26:13 AM UTC-7, Jussi Piitulainen wrote: > dave em writes: > > > > > He is asking a question I am having trouble answering which is how a > > > variable containing a value differs from a variable containing a > > > list or more specifically a list reference. > > > > My quite serious answer is: not at all. In particular, a list is a > > value. > > > > All those pointers to references to locations are implementation > > details. The user of the language needs to understand that an object > > keeps its identity when it's passed around: passed as an argument, > > returned by a function, stored in whatever location, retrieved from > > whatever location. Jessi, Thanks for your quick response. I'm still not sure we understand. The code below illustrates the concept we are trying to understand. Case 1: Example of variable with a specific value from P 170 of IYOCGWP >>> spam = 42 >>> cheese = spam >>> spam = 100 >>> spam 100 >>> cheese 42 Case 2: Example of variable with a list reference from p 170 >>> spam = [0, 1, 2, 3, 4, 5] >>> cheese = spam >>> cheese[1] = 'Hello!' >>> spam [0, 'Hello!', 2, 3, 4, 5] >>> cheese [0, 'Hello!', 2, 3, 4, 5] What I am trying to explain is this, why in case 1 when acting on spam (changing the value from 42 to 100) only affects spam and not cheese. Meanwhile, in case two acting on cheese also affects spam. Thanks and v/r, Dave -- https://mail.python.org/mailman/listinfo/python-list
Re: Explanation of list reference
All, Thanks for the excellent explanations and for sharing your knowledge. I definitely have a better understanding than I did this morning. Best regards, Dave -- https://mail.python.org/mailman/listinfo/python-list
converting strings to hex
Hello,
I am taking a cryptography class and am having a tough time with an assignment
similar to this.
Given plain text message 1 and cipher 1 compute cipher 2 for message 2
Work flow will be:
- figure out the key
- use key to compute c2
So this is what I have so far and am getting nowhere fast. I could use a
little help on this one.
So my first step is to compute the key. I suspect my error below is because c1
is a float and m1 is a string but I don't know how to turn the string into a
float.
Python 2.7###
m1text="my test message"
print( m1text + ' in hex is ')
print m1text.encode("hex")
m1 = m1text.encode("hex")
c1=0x6c73d5240a948c86981bc294814d
k=m1^c1
print( 'the key = ' )
print hex(k)
This code yields the following:
my test message in hex is
6d792074657374206d657373616765
Traceback (most recent call last):
File "/media/.../Crypto/Attackv2.py", line 10, in
k=m1^c1
TypeError: unsupported operand type(s) for ^: 'str' and 'long'
Any help is most appreciated.
Dave
--
https://mail.python.org/mailman/listinfo/python-list
Re: converting strings to hex
On Thursday, April 3, 2014 8:31:42 PM UTC-6, Tim Chase wrote:
> On 2014-04-03 19:10, dave em wrote:
>
> > So my first step is to compute the key. I suspect my error below
>
> > is because c1 is a float and m1 is a string but I don't know how to
>
> > turn the string into a float.
>
>
>
> For the record, "c1" in your example should be an integer/long
>
>
>
> It sounds like you want the optional parameter to int() so you'd do
>
>
>
> >>> hex_string = "My text message".encode("hex")
>
> >>> hex_string
>
> '4d792074657874206d657373616765'
>
> >>> m1 = int(hex_string, 16) # magic happens here
>
> >>> m1
>
> 402263600993355509170946582822086501L
>
> >>> c1=0x6c73d5240a948c86981bc294814d
>
> >>> c1
>
> 2199677439761906166135512011931981L
>
> >>> k = m1 ^ c1
>
> >>> k
>
> 400239414552556350237329405469124136L
>
> >>> hex(k) # as a string
>
> '0x4d1553a14172e0acebfd68b1f5e628L'
>
>
>
>
>
> -tkc
Thanks, got it. Sometimes the simple things can be difficult.
Dave
--
https://mail.python.org/mailman/listinfo/python-list
Re: converting strings to hex
> You haven't seen nothing yet, wait till M.L. catches you on the flip
>
> side for using gg. {running for cover}
Who is ML?
--
https://mail.python.org/mailman/listinfo/python-list
