我已经生成了一些代码,用于计算最小数量的20,10,5,2和1,它们将累计到用户定义的金额.用户只能输入整数,即没有小数值.我有两个问题.
printf语句的函数?我是新手,所以对他们来说有点失落.#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
int pounds;
int one, two, five, ten, twenty;
printf("Enter a pounds amount with no decimals, max E999999: \n");
scanf("%d", £s);
printf("%d\n", pounds);
if(pounds >= 20)
{
twenty = (pounds / 20);
pounds = (pounds-(twenty * 20));
printf("%d\n", pounds);
}
if(pounds >= 10)
{
ten = (pounds / 10);
pounds = (pounds-(ten * 10));
printf("%d\n", pounds);
}
if(pounds >= 5)
{
five = (pounds / 5);
pounds …Run Code Online (Sandbox Code Playgroud) 我有2张桌子,Facilities和Services.
CREATE TABLE Facilities (
facility_id NUMBER(2) NOT NULL,
facility_name VARCHAR2(20) NOT NULL,
CONSTRAINT pk_facil_id PRIMARY KEY (facility_id)
);
CREATE TABLE Services (
service_id NUMBER(2) NOT NULL,
service_name VARCHAR(20) NOT NULL,
service_facility NUMBER(2) NOT NULL,
CONSTRAINT pk_serviceid PRIMARY KEY (service_id)
);
ALTER TABLE Services
add CONSTRAINT fk_servicefacility FOREIGN KEY(service_facility)
REFERENCES Facilities(facility_id);
Run Code Online (Sandbox Code Playgroud)
如果我尝试将记录输入"服务"表,如下所示:
INSERT INTO Services (service_id, service_name, service_facility)
SELECT 06, 'Rooms',
(SELECT facility_id, FROM Facilities WHERE facility_name = 'Hotel')
FROM Dual;
Run Code Online (Sandbox Code Playgroud)
我在7个插入语句中有3个出现错误"缺少表达式".缺少什么表达?
在我的程序中,用户输入我在发送之前处理的术语.这个过程的一部分是将'and','或'和'not'的所有实例更改为大写字母,但保留其余部分完整.
我无法使用,string.upper()因为它将所有内容都改为大写; 或者string.replace()因为如果'和'在字符串中的另一个单词中,例如'salamander',它也会将其更改为'salamANDer'.我认为我最好的选择是正则表达式re.sub()功能.这允许我改变完整的单词.下一个问题:我必须为re.sub()我想做的每个更改执行一个函数.有可能做一个声明来做所有的改变吗?我所做的并没有错,但我认为它不一定是好的做法:
>>import urllib2
>>import re
>>query = 'Lizards and Amphibians not salamander or newt'
>>query=re.sub(r'\bnot\b', 'NOT',query)
>>query=re.sub(r'\bor\b', 'OR',query)
>>query=re.sub(r'\band\b', 'AND',query)
>>query = urllib2.quote("'"+query+"'")
>>print query
%27Lizards%20AND%20Amphibians%20NOT%20salamander%20OR%20newt%27
Run Code Online (Sandbox Code Playgroud) 我需要将字符串中的元音更改为使用C的$.我知道我需要使用for循环,我很确定我是在正确的轨道但我无法让它工作.
这是我的代码:
#include <stdio.h>
#include <string.h>
int main(void)
{
char input[50];
char i;
int j = 0;
printf("Please enter a sentence: ");
fgets(input, 50 , stdin);
for (j = 0; input[i] != '\0'; j++)
if (input[i]=='a'||input[i]=='e'||input[i]=='i'||input[i]=='o'||input[i]=='u')
{
input[i]= '$';
printf("Your new sentence is: %s", input);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我知道我的错误不是很大,但我看不出来.这是家庭作业,所以我不想要一个解决方案,只是一些建议,以便我可以从中学习.
编辑:
感谢那些家伙,我摆脱了'j',现在它可以运行但是当我运行程序时,它会为每个元音输出一个新行.我如何编码它以便它只输出最后一行,即所有的元音都改变了?
我正在使用XOR创建一个非常基本的加密程序来加密用户文件并再次解密.然而,正在发生的事情是,当我加密时,我得到相同的6个字母的长行,当我尝试解密时,它不起作用.谁能发现问题?另外,我想用我自己的基本加密方法替换XOR,但保留除密钥之外的相同代码.这有可能吗?谢谢!
#include <stdio.h>
int main(int argc, char*argv[])
{
FILE *fp1, *fp2;
char* key;
int c;
key = argv[1];
if(*key != '\0')
{
fp1 = fopen(argv[2],"rb");
if(fp1 != NULL)
{
fp2 = fopen(argv[3],"wb");
if (fp2 != NULL)
{
while((c=getc(fp1)!=EOF))
{
if(!*key)key = argv[1];
c ^=*(key++);
putc(c, fp2);
}
fclose(fp2);
}
fclose(fp1);
}
}
return 1;
}
Run Code Online (Sandbox Code Playgroud) 我有一个2d矩阵,可以是任何大小,但总是一个正方形.我想循环遍历矩阵,并为每个对角元素(x在示例中)我想分配值,1-sum_of_all_other_values_in_the_row例如
Mtx = [[ x ,.2 , 0 ,.2,.2]
[ 0 , x ,.4 ,.2,.2]
[.2 ,.2 , x , 0, 0]
[ 0 , 0 ,.2 , x,.2]
[ 0 , 0 , 0 , 0, x]]
for i in enumerate(Mtx):
for j in enumerate(Mtx):
if Mtx[i][j] == 'x'
Mtx[i][j] = 1-sum of all other [j]'s in the row
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚如何获得每一行中j的总和
我需要为嵌套字典中的每个项添加一个值增加1的键.我一直在尝试使用dict['key']='value'语法,但无法使其适用于嵌套字典.我确定这很简单.
我的词典:
mydict={'a':{'result':[{'key1':'value1','key2':'value2'},
{'key1':'value3','key2':'value4'}]}}
Run Code Online (Sandbox Code Playgroud)
这是将密钥添加到字典主要部分的代码:
for x in range(len(mydict)):
number = 1+x
str(number)
mydict[d'index']=number
print mydict
#out: {d'index':d'1',d'a'{d'result':[...]}}
Run Code Online (Sandbox Code Playgroud)
我想将新的键和值添加到方括号内的小字典中:
{'a':{'result':[{'key1':'value1',...,'index':'number'}]}}
Run Code Online (Sandbox Code Playgroud)
如果我尝试在最后一行添加更多图层,for loop我会收到一个回溯错误:
Traceback (most recent call last):
File "C:\Python27\program.py", line 34, in <module>
main()
File "C:\Python27\program.py", line 23, in main
mydict['a']['result']['index']=number
TypeError: list indices must be integers, not unicode
Run Code Online (Sandbox Code Playgroud)
我已经尝试了各种不同的方式列出嵌套项目但没有快乐.有人可以帮我从这里出去吗?
我正在使用CounterPython中的类来计算大型python字典的键中的单词数.我正在创建一个新的字典,其中键是递增的数字,值是计数器函数的返回值.这是我的新词典的样子:
TP = {1:Counter({u'x':1, u'b':1, u'H':3}),2:Counter({u'j':1, u'm':4, u'e':2})...}
Run Code Online (Sandbox Code Playgroud)
我想做的,如果可能的话,删除Counter和括号(和),以便我的字典看起来像:
TP = {1:{u'x':1, u'b':1, u'H':3}, 2:{u'j':1, u'm':4, u'e':2}...}
Run Code Online (Sandbox Code Playgroud)
如果那是不可能的,有人可以告诉我如何访问()里面的嵌套字典吗?谢谢
我正在编写一个项目管理Web应用程序,仅供练习.基本思想是用户可以将项目添加到应用程序,然后通过界面管理与项目相关的任务和约会.我目前正在设计数据库,我想知道最佳实践将在这里指示.
到目前为止我有4张桌子:
+----------+ +-------------+ +--------------+ +-------------+
|Users | |Projects | |Tasks | |Appointments |
+----------+ +-------------+ +--------------+ +-------------+
|id | |id | |id | |id |
|username | |project_name | |task_name | |appt_name |
|fname | |project_desc | |task_details | |appt_details |
|sname | | | |task_deadline | |appt_date |
+----------+ +-------------+ +--------------+ +-------------+
Run Code Online (Sandbox Code Playgroud)
我将基本关系视为:
user可以有很多projects,tasks和appointments.project可以有很多users,tasks和appointments.task可以有很多users,但只能与一个相关联project.A …python非常新,无法理解为什么这不起作用.我有一个在文本文件中逐行存储的Web地址列表.我想将前10个存储在名为bing的数组/列表中,将下一个10存储在名为yahoo的列表中,将最后10个存储在名为duckgo的列表中.我正在使用该readlines函数将文件中的数据读入每个数组.问题是没有写入列表.计数正在增加.此外,如果我完全删除循环并只是将整个文本文件读入一个列表,它可以完美地工作.这让我相信循环导致问题.我正在使用的代码如下.真的很感激一些反馈.
count=0;
#Open the file
fo=open("results.txt","r")
#read into each array
while(count<30):
if(count<10):
bing = fo.readlines()
count+=1
print bing
print count
elif(count>=10 and count<=19):
yahoo = fo.readlines()
count+=1
print count
elif(count>=20 and count<=29):
duckgo = fo.readlines()
count+=1
print count
print bing
print yahoo
print duckgo
fo.close
Run Code Online (Sandbox Code Playgroud)