我是Python的初学者,我正在编写一段代码,询问您的名字,并在程序的其余部分记住它。
print("What will you name yourself?")
name_input = input("> ")
name = name_input
print("Your name is: ") + name
错误跟踪消息是:
Traceback (most recent call last):
File "G:\#######\My Stuff\Deskemon.py", line 16, in <module>
以下是打印和计算加权考试成绩的代码:
def exam(weight,score_earned,score_shift):
if (score_shift==1):
shift_amount=input("What was the shift amount?")
score_earned=int(shift_amount+score_earned)
if (score_earned >100):
score_earned=100
print("Total points = "+st
所以我想要做的是从常用的库(如子进程、shutil等)中获取一些预先存在的函数.并将它们封装在一个函数中,该函数将输出该函数正在执行的操作以及发生错误时的错误。我目前的代码看起来像这样。
def copytree(self,*args, **kwargs):
self.debug(f":- Copying tree from \"{args[0]}\" to \"{args[1]}\"")
try:
shutil.copytree(*args,**kwargs)
except Exception as e:
问题就在这里,这是代码:
public class Chars
{
public static void main(String[] args){
Chars c = new Chars();
String res = c.test("abcd");
System.out.println(res);
}
public String test(String str){
String res = "";
res += str.charAt(0) + str.c
在Python3中,我想检查value是string还是None。
实现此目的的一种方法是
assert type(value) in { str, NoneType }
但是NoneType在Python中的位置是什么呢?
在不进行任何导入的情况下,使用NoneType生成NameError: name 'NoneType' is not defined。
我正在编写代码,通过GUI输入索引值:
import tkinter as tk
import tkinter.messagebox
#from tkinter import *
from PIL import Image, ImageTk #To display the image of the formulas of the methods
from tkinter.filedialog import askopenfile
root3 = tk.Tk()
root3.geometry('200x300')
info2=tk.Label(root3,text=
我编写了以下函数:
def _clean_dict(d):
return {k: v for k, v in d.items() if v is not None}
我想向函数中添加类型注释:
def _clean_dict(d: Dict[Any, Any]) -> Dict[Any, Any]:
return {k: v for k, v in d.items() if v is not None}
但是,我希望显式地定义返回字典中的值不能是None。
是否有一种方式来表示"Any类型,除NoneType“
如果函数可以返回None,类型注释不应该使用NoneType吗?
例如,我们不应该使用这个:
from types import NoneType
def my_function(num: int) -> int | NoneType:
if num > 0:
return num
return None
而不是:
def my_function(num: int) -> int | None:
if num > 0:
return num
return None
我一直试图从一个网页(zeit online,德国报纸)中提取多篇文章,为此我有一个我想从其中下载文章的urls列表,所以我不需要为urls爬行页面。
python的报纸包在解析单个页面的内容方面做得很棒。在下载所有文章之前,我需要做的是自动更改urls。不幸的是,我的编码知识是有限的,而且我还没有找到一种方法来做到这一点。如果有人能帮我,我会非常感激的。
我试过的一件事是:
import newspaper
from newspaper import Article
lista = ['url','url']
for list in lista:
f
这就是我想要做的:
class Base:
_type = None
name: str = _type.name
class a(Base):
_type = UnityType
a_instance = a()
a_instance.name # Expecting UnityType.name to be some string.
在尝试这个的时候,我得到了'NoneType' object has no attribute 'name'。原因很清楚,也是可以理解的。但是,如何将实现变量类的责任传递给子类呢?
我试着在Pig中的python udf下面运行
@outputSchema("word:chararray")
def get(s):
out = s.lower()
return out;
我得到了下面的错误:
File "/home/test.py", line 3, in get
out = s.lower()
AttributeError: 'NoneType' object has no attribute 'lower'
#Alrighty lets give this a shot.
#So it no longer works and i can't figure out why....
coach = 550
entry = 30
students = 45
maxstudents = 45
discount = 0
moneydiscount = 0
cost = 0
studentcost = 0
Run = True
while Run == True:
students = int(input("Please input number of students goi