我在向函数中提供值时遇到问题,并且没有将值类型设置为int,而是使用NoneType,我无法使用它。这是我抛出的错误信息:
Traceback (most recent call last):
File "NumberGame1.py", line 140, in <module>
main()
File "NumberGame1.py", line 29, in main
result = guessinggame(number)
File "NumberGame1.py", line 92, in guessinggame
if guess - number <= level * 0.1:
TypeError: unsupported operand type(s) for -: 'int' and 'NoneType'
以下是所有代码:
import random
import timeit
def main():
print "\nWelcome to Ethan's Number Guessing Game!"
print "What would you like to do?: \n1. Play game\n2. View High Scores\n3. View Credits"
menuchoice = input()
## choice is Menu Decision
if menuchoice == 1:
difficulty = input("Pick a difficulty!: \n1: Easy\n2: Medium\n3: Hard\n4: Insane\n5. Out of control!\n6. Custom\n")
leveldif = difcalc(difficulty)
## Exits program if you don't choose a difficulty
global level
level = leveldif[0]
difmod = leveldif[1]
number = numbergenerator(level)
result = guessinggame(number)
totalTime = result[0]
tries = result[1]
scorer(tries, totalTime)
elif menuchoice == 2:
## Figure out how to access high scores
print "This feature is currently under development.\n"
elif menuchoice == 3:
print "\nDeveloped by Ethan Houston"
raw_input()
print "\nDeveloped in Python 2.7.9 using Sublime Text 2"
raw_input()
print "\nThanks for playing :)"
raw_input()
## Simple credits reel. Go me
def difcalc(difficulty):
if difficulty == 1:
leveldif = [10, 1]
elif difficulty == 2:
leveldif = [50, 1.5]
elif difficulty == 3:
leveldif = [100, 2]
elif difficulty == 4:
leveldif = [1000, 10]
elif difficulty == 5:
leveldif = [10000, 20]
elif difficulty == 0:
leveldif = [1, 1]
return leveldif
def guessinggame(number):
tries = 1
## Counter for number of attempts at guessing
guess = input("Guess a number between 1 and " + str(level) + "!: ")
## Takes input from user
while guess > level:
guess = input("Above range!\nMake sure to guess between 1 and " + str(level) + "!: ")
## If the user chooses a number above the range, makes you try again until within range
startTime = timeit.default_timer()
## Starts a timer once first valid number is selected
while guess != number:
## While loop that runs as long as guess isn't correct
if guess > number:
if guess - number <= level * 0.1:
guess = input("Too high, close!: ")
tries += 1
## If difference between guess and answer is less than or equal to 10% of level,
## prints different prompt
else:
guess = input("Too high, guess again: ")
tries += 1
## Normal behavior
elif guess < number:
if guess - number <= level * 0.1:
guess = input("Too low, close!: ")
tries += 1
## If difference between guess and answer is less than or equal to 10% of level,
## prints different prompt
else:
guess = input("Too low, guess again: ")
tries += 1
## Normal behavior
endTime = timeit.default_timer()
## Takes the time after the correct number is chosen
totalTime = endTime - startTime
## Calculates time difference between start and finish
result = [totalTime, tries]
return result
def numbergenerator(level):
global number
number = random.randint(1, level)
def scorer(tries, totalTime):
print "\nCorrect! It took you " + str(round(totalTime, 2)) + " seconds and " \
+ str(tries) + " tries to guess.\n"
## Once user guesses correct number, program tells how many tries it took, and how long
score = 1/(1+(tries * round(totalTime, 2))) * 1000 * difmod
## Calcualtes score, making lower times and fewer tries yield a higher score
## Difmod takes into account the difficulty
## Multiply by 1000 to make number more readable
print "Score: " + str(round(score, 2)) + "\n"
## Printsthe score, rounded to 1 decimal place
main()
答案 0 :(得分:3)
当python函数没有显式返回任何内容时,它返回None
。在您的情况下,您有一个功能 -
def numbergenerator(level):
global number
number = random.randint(1, level)
这不会返回任何内容,而是设置全局变量number
。
但是当你在main()
中调用此函数时,你会这样做 -
number = numbergenerator(level)
这里number
不是全局变量(即使它是全局变量也没关系),所以在调用numbergenerator()
后,由于它不返回任何内容,它返回{{1 },它被设置为None
变量,因此number
为None,导致您遇到的问题。
我相信你不需要在那里使用number
,你应该从global variable
函数返回数字,例如 -
numbergenerator()