473,387 Members | 1,650 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,387 software developers and data experts.

Problem with function

Ok, so i've been trying to learn how to use def function. I tried to make a simple archery game based on chance.

Expand|Select|Wrap|Line Numbers
  1. import random
  2. loop=1
  3. HP=100
  4. def shot(z):
  5.   if z<50:
  6.    return 50
  7.    print ("Direct Hit")
  8.   elif z >= 50:
  9.    print ("Missed")
  10. while loop==1:
  11.  h=random.random()*100
  12.  n=shot(h)
  13.  choice=raw_input("Pick a shot type(shot, headshot)")
  14.  if choice == "shot":
  15.   HP=100-n
  16.  if HP<=0:
  17.   print ('You win')
  18.   loop=0
  19.  print (HP)
  20.  print (h)
If the shot misses, then this happens

Traceback (most recent call last):
File "/home/usr/Desktop/archery.py", line 15, in <module>
HP=100-n
TypeError: unsupported operand type(s) for -: 'int' and 'NoneType'
I have no idea what I did wrong. Any help would be much appreciated.
Jun 29 '10 #1
2 1335
bvdet
2,851 Expert Mod 2GB
shot() returns None when "z" >= 50, therefore the error. You must return a number. Example:
Expand|Select|Wrap|Line Numbers
  1. >>> def x():
  2. ...     print "X"
  3. ...     
  4. >>> 100-x()
  5. X
  6. Traceback (most recent call last):
  7.   File "<interactive input>", line 1, in ?
  8. TypeError: unsupported operand type(s) for -: 'int' and 'NoneType'
  9. >>> def x():
  10. ...     print "X"
  11. ...     return 50
  12. ... 
  13. >>> 100-x()
  14. X
  15. 50
  16. >>> 
Jun 30 '10 #2
dwblas
626 Expert 512MB
If you are trying to update a score then it would be done like this.
Expand|Select|Wrap|Line Numbers
  1. def shot(z, score):
  2.     if z < 50:
  3.         score += 50
  4.         print ("Direct Hit")
  5.  
  6.     ## z >= 50 is redundant as that is the only possibility
  7.     else:
  8.         print ("Missed")
  9.  
  10.     return score
  11.  
  12. score = 0
  13. loop = 1
  14. while loop==1:
  15.     h=random.random()*100
  16.     score=shot(h, score)
  17. ## etc. 
Jun 30 '10 #3

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

Similar topics

5
by: asim | last post by:
Hi if a declare a variale say Dim Flag and assing it to true a boolean value Flag = True and if i try to concatinate it with another string like mystr="<my-str>" & Flag & "</my-str>" and if...
1
by: fig000 | last post by:
Hi, I have some code written into the html in a dot.net form: <%if test(Container.DataItem("user_id")) =2%> I've been able to use container.dataitem in these <%%> pairs as an expression to...
1
by: Lewap | last post by:
Hi! I have a problem: Function CreateNamedPipe (a piece of source below) called in a NT service I get an error 997 - Overlapped I/O operation is in progress. When I run the same code as...
0
by: papigreg | last post by:
Hi, I have made a Windows Forms .Net Project in C++. I use the Philips Speech SDK to make voice recognition on WAV files. In the recognition function, I must pass a pointer of a function which I...
21
by: Mike | last post by:
I am having a problem getting AND to work. I have a MySQL database field defined as varbinary(8) with X00 in it. Then I have define statements with X01 in it. define('CERTACCESS_MEDALS', x01); ...
5
by: dost | last post by:
as in a class private section we can access the private data member in main through the pointers...is their any way to access the member functions which are written in private section..... ...
9
by: lev | last post by:
Attempting to use javascript's regular expression methods to match a particular lab number and building which would then be copied to a pair of textfields, I found that general matching characters...
1
by: john.swilting | last post by:
I am not good in Javascript I begin I do not understand why my function does not go I post my code <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">...
4
by: poojak | last post by:
I have write two different js function in a external file. one function write a table (I used innerHTML for this.) and another function is for showing/ hideing some rows depending on radio button...
3
by: ziycon | last post by:
The below function works properly when the its called by: storeExtraImages("n","news",1); But when called by these next two line's it gets to if(move_uploaded_file($_FILES,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.