def count(string,letter):
count=0
for each in string:
if each==letter:
count=count+1
print count
Run Code Online (Sandbox Code Playgroud)
有没有办法设置参数以自动接受字符串形式的输入,这样用户就不必添加引号?
我的代码在底部:(如果有人有足够的耐心阅读全部内容,我会感到非常惊讶)
它输出了错误的数字,但我不知道为什么。我遵循了算法。输入必须是 2 个一元数字,例如 2 和 6 => 01101111110。如果有更简单的方法来编写此算法,请告诉我。这是算法(来自罗杰·彭罗斯的《皇帝的新思想》一书)
我的代码是:
x = input("Dos numeros en sistema unario separado por un 0: ")
x = list(x)
pos = 0
valorMaq = 0
while (True):
if valorMaq==0 and x[pos]=='0':
valorMaq=0
x[pos]='0'
pos=pos+1
print(x)
elif valorMaq==0 and x[pos]=='1':
valorMaq=11
x[pos]='0'
pos=pos-1
print(x)
elif valorMaq==1 and x[pos]=='0':
valorMaq=10
x[pos]='1'
pos=pos+1
print(x)
elif valorMaq==1 and x[pos]=='1':
valorMaq=1
x[pos]='1'
pos=pos-1
print(x)
############################################
elif valorMaq==10 and x[pos]=='0':
valorMaq=1010
x[pos]='0'
pos=pos+1
print(x)
elif valorMaq==10 and x[pos]=='1':
valorMaq=11
x[pos]='0' …Run Code Online (Sandbox Code Playgroud) 有没有什么办法可以像matplotlib一样使用plotly,也就是使绘图显示在弹出窗口中?例如,是否有一个简单的等价物
plt.plot([1,2,3], [2, 3, 2.5])
plt.show()
Run Code Online (Sandbox Code Playgroud)
我尝试了各种功能,但是它们似乎都可以创建html文件或图像文件。
b1 = Button(tk, text=" ", font=("Consolas 30 bold"), height = 1, width = 4,bg = "white", command=lambda:checker(b1))
Run Code Online (Sandbox Code Playgroud)
我要添加什么代码来为按钮添加边框,使其变粗?
我从文件中读取行,例如:
\n\n\n\n\n小大事:163 Wege zur Spitzenleistung (Dein Leben)(德语版)(彼得斯、汤姆)
\n\nDie madlle Katastrophe: So f\xc3\xbchren Sie Teams \xc3\xbcber Distanz zur\n Spitzenleistung (德语版) (Thomas, Gary)
\n
我用以下方法读取/编码它们:
\n\ntitle = line.encode(\'utf8\')\nRun Code Online (Sandbox Code Playgroud)\n\n但输出是:
\n\n\n\n\nb\'Die 美德灾难:所以 f\\xc3\\xbchren Sie Teams \\xc3\\xbcber\n Distanz zur Spitzenleistung(德语版)(托马斯、加里)\'
\n\nb\'The Little Big Things: 163 Wege zur Spitzenleistung (Dein Leben)\n(德语版)(Peters,Tom)\'
\n
为什么总是添加“b\'”?\n如何正确读取文件以便保留“元音变音”?
\n\n这是完整的相关代码片段:
\n\n# Parse the clippings.txt file\nlines = [line.strip() for line in codecs.open(config[\'CLIPPINGS_FILE\'], \'r\', \'utf-8-sig\')]\nfor line in lines:\n line_count = line_count …Run Code Online (Sandbox Code Playgroud) 我试图让 Button1 在按下时将其上的文本从“hi”更改为“bye”,并在第二次按下时再次更改回来。
这是我的代码:
from tkinter import *
def toggletext():
if Button1["text"] == "hi":
Button1["text"] = "bye"
Game.update()
else:
Button1["text"] = "hi"
Game.update()
Game = Tk()
Game.wm_title("title")
Button1 = Button(text="hi",fg="white",bg="purple",width=2,height=1,command=toggletext).grid(row=0,column=0)
Button2 = Button(fg="white",bg="purple",width=2,height=1).grid(row=1,column=0)
Button3 = Button(fg="white",bg="purple",width=2,height=1).grid(row=0,column=1)
Button4 = Button(fg="white",bg="purple",width=2,height=1).grid(row=1,column=1)
Game.mainloop()
Run Code Online (Sandbox Code Playgroud)
当我按下 Button1 时出现此错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
return self.func(*args)
File "C:\Users\User1\Desktop\gridtest2.py", line 4, in toggletext
if Button1["text"] == "hi":
TypeError: 'NoneType' object is not subscriptable
Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Matlab 中执行以下代码我有两组 A 和 B,其中 A={1,2,5,6,10} 和 B={2,3,5,7,9,11} 当我尝试下面的代码给出了一个错误:
A =[1,2,5,6,10]
B =[2,3,5,7,9,11]
C = union( A,B )
D = intersect(A,B)
Result = D/C
Run Code Online (Sandbox Code Playgroud)
这是错误
Error using /
Matrix dimensions must agree.
Error in code (line 6)
Result = D/C
Run Code Online (Sandbox Code Playgroud)
谁能帮我先计算|A?B|然后|AUB| 在 Matlab 中。最后,我将把 (|A?B|) / (|AUB|) 都分开。
while True:
num=raw_input("Please enter a number.")
if (num == 1):
print "Sunday"
elif (num==2):
print "Monday"
elif (num==3):
print "Tuesday"
elif (num==4):
print "Wednesday"
elif (num==5):
print "Thursday"
elif (num==6):
print "Friday"
elif (num==7):
print "Saturday"
else:
print "Invalid Choice!"
option = raw_input("Would you like to continue playing?")
if (option=="yes"):
continue
elif (option=="no"):
break
Run Code Online (Sandbox Code Playgroud)
这是我的代码.当我由于某种原因运行它时,第一部分(一周的那一天)的输出作为"其他"选项出现,即"无效选择".当我删除else语句时,输出只是空白.稍微混淆了为什么会发生这种情况.
我有一个二维图,它看起来是二次的。我从通过计算以数字方式获得的数据集中得到它。我知道可以通过拟合数据集来获得方程。Python 似乎自动根据数据点进行拟合。我需要打印拟合曲线的方程。
我对不同的 X 求解 Y 并获得两个数组 Y 和 X。然后我将它们绘制出来
plt.plot(X,Y)
plt.xlabel('X')
plt.ylabel('Y')
plt.savefig('YvsX.png', format='png', dpi=1000)
plt.show()
Run Code Online (Sandbox Code Playgroud)
并得到这个:
需要打印该图的方程