我正在尝试用Python编写程序,但我被困在这段代码中:
def function():
a=[3,4,5,2,4]
b=1
c=0
for x in range(5):
if a[x-1]>b:
c=c+1
return c
print(function())
Run Code Online (Sandbox Code Playgroud)
它给了我1而不是5的值.实际上我想写的函数有点复杂,但问题实际上是一样的,它没有给我正确的结果.
def result():
r=[0]*len(y)
a=2
b=an_integer
while b>0:
for x in range(len(y)) :
if y[x-1] > 1/a and b>0:
r[x-1]=r[x-1]+1
b=b-1
a=a+1
return r
print(result())
Run Code Online (Sandbox Code Playgroud)
v是小于1的值列表,b具有整数值.如果v中的某些值大于1/a那么r中的值x应该大1,那么它应该重复a = a + 1直到b变为0.我希望这个函数给出ex的类型结果.[7,6,5,4,3]其中此列表中元素的总和等于b.
有时候它给了我正确的值,有时候没有,当v中的元素相等时,例如v = [0.33333,0.33333,0.33333]它会卡住并且不会给我一个结果.
我不知道我做错了什么!
我在页面中有多个(引导程序)按钮,我只希望其中一个按钮在单击时改变颜色.因此,如果先前单击了一个按钮并接收了另一种颜色,现在单击一个新按钮,则前一个按钮应返回正常颜色,新按钮应更改.(我希望我解释得对)
我现在可以使用JQuery来更改元素类,例如.单击时从btn-primary更改为btn-success类(从而更改颜色),但这需要按钮具有ID.
$("#td_id").attr('class', 'newClass');
Run Code Online (Sandbox Code Playgroud)
1)那么,我如何迭代所有按钮,检查他们的课程并在需要时进行更改?
2)有更优雅的解决方案吗?
我正在尝试编写一个显示文件内容的简单bash脚本.
#!/bin/bash
echo 'Input the path of a file or directory...'
read File
if [ -e $File ] && [ -f $File ] && [ -r $File ]
then
echo 'Displaying the contents of the file '$File
cat $File
elif [ -d $File ] && [ -r $File ]
then
echo 'Displaying the contents of the directory '$File
for FILE in `ls -R $File`
do
cd $File/$FILE
echo 'Displaying the contents of the file '$FILE
cat $FILE
done
else
echo …Run Code Online (Sandbox Code Playgroud)