我正在使用django:
我正在尝试将元组列表传递views.py
到下拉框形式但我得到此属性错误
forms.py
import logging
from django import forms
log = logging.getLogger(__name__)
class TestForm(forms.Form):
def __init__(self, *args, **kwargs):
testlist = kwargs.pop('testlist',None)
log.info(regionlist)
self.fields['testlist'] = forms.ChoiceField(choices=testlist)
super(TestForm, self).__init__(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)
views.py
form = forms.RegionForm(regionlist=data)
Run Code Online (Sandbox Code Playgroud)
我使用正确的方法在views.py
和之间传递变量forms.py
吗?
我怎样才能读取文件中以python中的数字开头的行,即
Hello World <--ignore
Lovely day
blah43
blah
1234 <--read
blah12 <--ignore
blah
3124 <--read
Not matter how many words there are <--ignore
blah
0832 8423984 234892304 8239048324 8023948<--read
blah132 <--ignore
Run Code Online (Sandbox Code Playgroud) 我创建了一个程序,为用户输入一个输入,然后让他们输入他们想要输入的数量,但是当我打印总数和金额时.然而,这正在全面反转相同的价值:即
总计:5.00食物:5.00票据:5.00旅行:5.00 fags:5.00
代替:
总计:14.00食物:2.00票据:3.00旅行:4.00 fags:5.00
int main(void)
{
float food = 0.00;
float travel = 0.00;
float bills = 0.00;
float fags = 0.00;
float total = 0.00;
float t_food, t_travel, t_bills, t_fags;
char userInput[3];
while(userInput[0] != 'X')
{
printf("Select option,\nA: Food\nB: Travel\nC: Bills\nD: Fags\nX: Exit\n");
scanf("%s", userInput);
if((userInput[0] == 'A') || (userInput[0] =='a'))
{
printf("Please enter an amount: ");
scanf("%f", &food);
printf("You have entered: %.2f\n", food);
t_food += food;
}
if((userInput[0] == 'B') || (userInput[0] =='b'))
{
printf("Please …
Run Code Online (Sandbox Code Playgroud) 我正在编写一个程序来读取另一个程序的输出,逐行读取并将其放入列表中.
#!/usr/bin/python
import subprocess
def RECEIVE(COMMAND):
PROCESS = subprocess.Popen(COMMAND, stdout=subprocess.PIPE)
LINES = iter(PROCESS.stdout.readline, "")
for LINE in LINES:
RECARR = LINE.split()
print RECARR[14]
RECEIVE(["receivetest","-f=/dev/pcan32"])
Run Code Online (Sandbox Code Playgroud)
receivetest程序的输出是:
19327481.401 receivetest: m s 0x0000000663 8 2f 00 42 02 00 e4 8a 8a
19327481.860 receivetest: m s 0x000000069e 8 00 1f 5e 28 34 83 59 1a
Run Code Online (Sandbox Code Playgroud)
它是一个不断的消息流.拆分时,列表的范围是14,因为在拆分后,为了确保,我使用了:
print len(RECARR)
Run Code Online (Sandbox Code Playgroud)
这给了我一个输出14
.
但每当我尝试打印最后一个元素时:
print RECARR[14]
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
file "./cancheck.py", line 10, in RECEIVE
print RECARR[14]
IndexError: list index out of range
Run Code Online (Sandbox Code Playgroud)
这是由于列表顶部打印的一些错误文本引起的,所以我需要一些方法来确保程序只读取以
1234567.123
/^(.......\.\d{1,3}) …
Run Code Online (Sandbox Code Playgroud) 我是C的新手,我正在尝试使用int数组作为这样的开关的选项号
void totalEntered(float * total, float * inputFigure)
{
*total = *total + *inputFigure;
}
int main()
{
float amountEntered = 0.00;
float tot = 0.00;
int option_numbers[3] = {1,2,3,4};
float a_food = 0.00;
float a_tab = 0.00;
float a_trav = 0.00;
float a_bill = 0.00;
totalEntered(&tot, &amountEntered);
printf("Please enter option number from below \n1.food\n2.tabacco\n3.travel\n4.bills\n");
scanf("%i", option_numbers);
switch(option_numbers)
{
case 1:
printf("Please Enter an amount: ");
scanf("%f", amountEntered);
a_food = a_food + amountEntered;
totalEntered(&tot, &amountEntered);
printf("You have spent %f on …
Run Code Online (Sandbox Code Playgroud)