473,320 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Why Doesn't This Code Work, Python 3.0

I'm finding it hard to make this code work in Python 3.0. Been looking at it for some time now:

Expand|Select|Wrap|Line Numbers
  1. # import module for random functions
  2. import random
  3.  
  4. # List of words for the computer to pick from
  5. words = ("basketball", "football", "hockey", "rugby", "baseball")
  6.  
  7. # Word to be guessed; picked at random 
  8. word = random.choice(words)
  9. letters_guessed = []
  10.  
  11. print ("Guess the sport!")
  12. print ("You get to give five letters.")
  13. print ("There are %s letters in the word.") % (len(word))
  14. guesses = 5
  15. while guesses != 0:
  16. letter = input("Enter a letter: ")
  17. if letter in letters_guessed:
  18. print ("You already guessed that letter.")
  19. else:
  20. guesses = guesses - 1
  21. print ("You have %d guesses left.") % (guesses)
  22. letters_guessed.append(letter)
  23.  
  24.  
  25. print ("The word:")
  26.  
  27. masked_word = ""
  28. for letter in word:
  29. if letter in letters_guessed:
  30. masked_word += letter
  31. else: masked_word += "-"
  32. print masked_word
  33. guess = input("Guess the word: ")
  34. if guess == word:
  35. print ("Congratulations, %s is the word!") % (guess)
  36. else:
  37. print ("Nope. The word is %s.") % (word)
  38. input ("Press The Enter Key To Exit")
Jan 4 '10 #1
7 2656
bvdet
2,851 Expert Mod 2GB
Please use code tags when posting code. See posting guidelines here.

BV - Moderator
Jan 4 '10 #2
bvdet
2,851 Expert Mod 2GB
jharrison,

You have no indentation in the code you posted. It could be as simple as that. What error message do you get when you run the code?
Jan 4 '10 #3
okay i have the issue solved, when it runs now I get this error message.


Guess the sport!
You get to give five letters.
There are %s letters in the word.
Traceback (most recent call last):
File "C:\Users\Jamo\Guess The Word.py", line 13, in <module>
print ("There are %s letters in the word.") % (len(word))
TypeError: unsupported operand type(s) for %: 'NoneType' and 'int
Jan 4 '10 #4
bvdet
2,851 Expert Mod 2GB
I was hoping that you would repost your code using code tags. There is no indentation in the code you posted. That makes it difficult to follow.
Jan 5 '10 #5
bvdet
2,851 Expert Mod 2GB
I converted to code to run in Python 2.5 and guessed at the indentation:
Expand|Select|Wrap|Line Numbers
  1. # import module for random functions
  2. import random
  3.  
  4. # List of words for the computer to pick from
  5. words = ("basketball", "football", "hockey", "rugby", "baseball")
  6.  
  7. # Word to be guessed; picked at random 
  8. word = random.choice(words)
  9. letters_guessed = []
  10.  
  11. print "Guess the sport!"
  12. print "You get to give five letters."
  13. print "There are %s letters in the word." % (len(word))
  14. guesses = 5
  15. while guesses != 0:
  16.     letter = raw_input("Enter a letter: ")
  17.     if letter in letters_guessed:
  18.         print "You already guessed that letter."
  19.     else:
  20.         guesses = guesses - 1
  21.         print ("You have %d guesses left.") % (guesses)
  22.         letters_guessed.append(letter)
  23.  
  24.     print "The word:"
  25.     masked_word = ""
  26.     for letter in word:
  27.         if letter in letters_guessed:
  28.             masked_word += letter
  29.         else: masked_word += "-"
  30.     print masked_word
  31.  
  32. guess = raw_input("Guess the word: ")
  33. if guess == word:
  34.     print "Congratulations, %s is the word!" % (guess)
  35. else:
  36.     print "Nope. The word is %s." % (word)
  37. raw_input ("Press The Enter Key To Exit")
It seems to work.
Jan 5 '10 #6
I think the problem is the parentheses in your print statement. Try changing this:

Expand|Select|Wrap|Line Numbers
  1. print ("There are %s letters in the word.") % (len(word))
To this:
Expand|Select|Wrap|Line Numbers
  1. print ("There are %s letters in the word." % len(word))
That way the print function gets just one argument, an expression that evaluates to the finished string.
Jan 5 '10 #7
bvdet
2,851 Expert Mod 2GB
Thanks for the input sockmonk. Invalid parentheses around arguments to the print function would be consistent with the error message. I should have noticed it.
Jan 5 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Vin Jovanovic | last post by:
Trying to start Python IDLE through Start>Programs>Python 2.3> IDLE doesn't work From DOS ... I get this .... C:\>python C:\Python23\Lib\idlelib\idle.py Traceback (most recent call last):...
1
by: Erick Bodine | last post by:
I am trying to set a new environment variable on a W2k machine with only partial success. The name("SSID") and value("ASIM") show up correctly in the registry and when I go to "System...
6
by: Peter Abel | last post by:
I have an application, which is an instance of a class with a deeply nested object hierarchy. Among others one method will be executed as a thread, which can be stopped. Everything works fine...
14
by: Antoon Pardon | last post by:
The queue and condition class allow threads to wait only a limited time. However this currently is implemented by a polling loop. Now for those who like to avoid polling I have here a Tlock...
11
by: ritterhaus | last post by:
Just a simple bit of code to toggle between two state at intervals... import time for i in range(4): print 'On' time.sleep(1) print 'Off' time.sleep(1) .... SHOULD toggle On and Off four...
149
by: Christopher Benson-Manica | last post by:
(Followups set to comp.std.c. Apologies if the crosspost is unwelcome.) strchr() is to strrchr() as strstr() is to strrstr(), but strrstr() isn't part of the standard. Why not? --...
5
by: Dongsheng Ruan | last post by:
I want to turn an Array into a heap, but my code just doesn't work: no change after execution. A= m=len(A)-1 for i in range(m,1): t=(i-1)/2
6
by: Stef Mientki | last post by:
hello, I want to find all files with the extension "*.txt". From the examples in "Learning Python, Lutz and Asher" and from the website I see examples where you also may specify a wildcard...
0
by: Riccardo Di Meo | last post by:
Hi everyone, I'm practicing with embedding python into C code and i have encountered a very strange problem: I'm unable to call the "accept" method of a (correctly created) server socket without...
0
by: cnb | last post by:
Python-mode worked right out out of the box in Emacs and then I added Python to my path so now the interpreter works as well. However when I try to load a file(that is not located in site-...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.