我刚刚学习(正在学习)函数参数如何在Python中工作,我开始尝试使用它没有明显的原因,当这个:
def jiskya(x, y):
if x > y:
print y
else:
print x
print(jiskya(2, 3))
Run Code Online (Sandbox Code Playgroud)
给出了输出:
>>>
2
None
Run Code Online (Sandbox Code Playgroud)
它None来自哪里?还有,这是什么?
很抱歉,我并不擅长 python,但我的问题是我需要在
这里替换一个字符,这是我想要更改的所有我需要更改的是 # 为所有行的 A
def puzzle():
print ("#+/084&;")
print ("#3*#%#+")
print ("8%203:")
print (",1$&")
print ("!-*%")
print (".#7&33&")
print ("#*#71%")
print ("&-&641'2")
print ("#))85")
print ("9&330*;")
Run Code Online (Sandbox Code Playgroud)
所以这是我试图做的(它在另一个 py 文件中)
from original_puzzle import puzzle
puzzle()
result = puzzle()
question = input("first letter ")
for letter in question:
if letter == "a":
result = result.replace("#","A")
print (result)
Run Code Online (Sandbox Code Playgroud)
这是它给我的
Traceback (most recent call last):
File "N:\AQA 4512;1-practical programming\code\game.py", line 36, in <module>
result = result.replace("#","A")
AttributeError: 'NoneType' object has no attribute …Run Code Online (Sandbox Code Playgroud) 规格:Ubuntu 13.04 Python 3.3.1
背景:Python初学者; 搜索了这个问题但我找到的答案更多的是"什么"而不是"为什么";
我打算做什么:创建一个功能,从用户输入测试分数输入,并根据年级比例/曲线输出字母等级; 这是代码:
score = input("Please enter test score: ")
score = int(score)
def letter_grade(score):
if 90 <= score <= 100:
print ("A")
elif 80 <= score <= 89:
print ("B")
elif 70 <= score <= 79:
print("C")
elif 60 <= score <= 69:
print("D")
elif score < 60:
print("F")
print (letter_grade(score))
Run Code Online (Sandbox Code Playgroud)
这在执行时返回:
None
(letter_grade(score)
print (letter_grade(score))
"无"并非意图.而且我发现,如果我使用None而不是None,则"无"不再出现.
我能找到的最接近的答案说"像python中的函数返回None,除非明确指示不这样做".但我确实在最后一行调用了一个函数,所以我在这里有点困惑.
所以我想我的问题是:是什么导致了"无"的消失?我确信这是非常基本的东西,但我无法找到解释"幕后"机制的任何答案.所以,如果有人能对此有所了解,我将不胜感激.谢谢!
我目前正在为我的计算机科学入门课程编写一个简短的程序,尽管我很确定我的定义很明确,但我的代码返回“none”。不要介意我的函数和东西的笨重命名,这是课程要求。代码的目的是可以选择一个形状,然后直接输入需要的信息,不需要书面提示,然后程序会返回选择的形状的面积。在过去的几个小时里,我一直在为此折腾,玩弄它,但无论我做什么,我的代码都没有返回。有什么建议吗?请不要公然给我新代码,因为我可能会因此而惹上麻烦,也许只是指出我的问题方向。
import math
# the following functions are built to calculate each shape
def circle_area(rad):
return math.pi*rad**2
def rectangle_area(side_one, side_two):
return side_one*side_two
def triangle_area(edge):
return (math.sqrt(3)/4)*(edge**2)
# the following function as assigned using the above functions
def shape_area():
shape_choice = input("Choose shape (1=circle, 2=rectangle, 3=triangle):")
if shape_choice == 1 or 3:
length_one = input("")
elif shape_choice == 2:
length_two, length_three = input("")
if shape_choice == 1:
circle_area(length_one)
elif shape_choice == 2:
rectangle_area(length_two, length_three)
elif shape_choice == 3:
triangle_area(length_one)
elif shape_choice …Run Code Online (Sandbox Code Playgroud)