这里是Python noob。我试图创建一个包含用户输入的数字的列表,然后在with循环中用列表中的数字进行一些简单的计算。输入“done”时,While循环不会中断。它只打印“无效输入”
list = []
while True:
try:
n = int(input('Enter a number: '))
list.append(n)
except:
print('Invalid input')
if n == 'done':
break
print
我正在写一个简单的计算器程序,允许用户将整数列表添加到python语法中作为一种条目。我希望该程序允许用户添加他们想要的数字在一起。我的错误是:
Traceback (most recent call last):
File "Calculator.py", line 17, in <module>
addition = sum(inputs)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
我的代码是:
#declare variables
in
我认为,Java applet有一个工作代码。然而,我并没有按照我想要的方式来执行我的程序。
下面是它运行时显示的消息(关于解析JTextField输入):
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unkno
在下面的ipython3会话中,我读取了不同格式的表,并对其中一列中的值进行了求和:
In [278]: F = pd.read_table("../RNA_Seq_analyses/mapping_worm_number_tests/hisat2/mapped_C_elegans/feature_count/W100_1_on_C_elegans/protein_coding_fwd_counts.txt", skip
...: rows=2, usecols=[6]).sum()
In [279]: S = pd.read_table("../RNA_S
尝试创建一个基本的加法函数,将输入的数字相加并输出总数。以下是我目前的功能:
function addition() {
read -a arr
for i in ${arr[@]}
do
str=$str'+'$i
echo $i
done
echo $str
}
但这似乎需要一个输入,然后输出上述内容。我也尝试过使用:
function addition() {
for i in ${input[@]}
但它输出一个空字符串。如何创建接受以下命令的函数:
$ addition 4 3 13
下面的代码使用python 2.x而不是python 3.x执行。
df = pd.DataFrame(data)
gb = df.groupby(['style'])
for a, b in gb:
#any command, iteration fails
错误是:
Traceback (most recent call last):
File "/root/anaconda3/lib/python3.4/site-packages/pandas/core/algorithms.py", line 143, in factorize
so
我不知道为什么这个代码是错误的:
int sum = 0;
// take sum of every other digit starting with second-to-last and add to sum variable
for (int i = length; i > 0; i -= 2)
{
sum += cardString[i];
printf("%c | %i\n", cardString[i], sum);
}
假设长度是15,"cardString“字符串是378282246310005,
我是python的新手,我遇到了一个场景,解释如下:
这是我所指的学习.pdf中的一个。如果任何人都能指导或分享一些其他资源,那就太好了。
一种重复读取数字直到用户输入“完成”的程序。输入“完成”后,打印出总数、计数和数字的平均值。如果用户输入的不是数字,请使用try和except检测他们的错误,并打印一条错误消息并跳到下一个数字。
Enter a number: 4
Enter a number: 5
Enter a number: bad data
Invalid input
Enter a number: 7
Enter a number: done
16 3 5.3333333333
我有一个dataframe,其中没有几个列是对象,我希望将其中一个列转换为int列,这样我就可以使用它了。做些计算。但是,当你试图去做的时候,它就是得到了这个错误。
这是我的密码。
给了我错误的代码。
df['Amount in USD']=df['Amount in USD'].str.replace(',', '') #this worked fine
df['Amount in USD']=df['Amount in USD'].astype(int) #but this doesn'
首先,我搜索并找到了一些其他的解决方案,这些解决方案看起来非常可信,而且应该有效,但无论出于什么原因,在用我的数据进行试验之后--这些解决方案都不起作用。我正在使用python 2.7.6
我正在尝试将字符串列表转换为整数,以便添加它们。
#!/usr/bin/env python
import re
numlist = list()
total = 0
#open the file
f = open('c:/Users/Home/Documents/homework/python_webdata/regex_sum_21441.txt', 'r')
for
有两个名为unitprice.txt和quantity.txt的文本框。还有一个名为total.txt的文本框,当用户输入单价和数量时,它会不断更新。
一旦用户输入这两个文本框,这两个文本框就会变空,total.txt会根据总数进行更新。它需要不断地进行,但在我的代码中并不是这样的,我说
INPUT STRING WASN'T IN A CORRECT FORM.
int tot = 0;
int sum = 0;
tot = int.Parse(unitprice.Text) * int.Parse(quantitytxt.Text);
sum = int.Parse(tota
我是Python新手。我试图在excel列中插入一个公式。公式的输入不一定来自一行。我有-
formula= '=B4-(28.471-0.0146*B4+0.0008*B4^2)+D2'
formulaRegex= re.compile(r'([A-Z]+)(\d{1,})')
cellCoordinatesR= formulaRegex.findall(formula) #find a Cap. Letter and a number
rowIndex=[] #list of the row indexes i.e '4' in '
是的,我在谷歌上做了“如何得到数组元素的和”,但是我想知道这里出了什么问题?如果我使用断点并在执行这一行后看到"sum = sum + (int) Ai;“sum有一些模糊的值。
#import <Foundation/Foundation.h>
@interface MyClass : NSObject
@end
@implementation MyClass
@end
int solution(NSMutableArray *A)
{
int sum = 0;
for (int i = 0; i <A.count; i ++)
{
NSL
我对编程相当陌生。我正在制作一个程序,您在文本框中输入一个数字,当您离开文本框时,它会将您的号码转换为货币。我还添加了另一个文本框来做同样的事情。
这就是我想要发生的:
If TextBox1.Text + TextBox2.Text =< 10,000 Then
Sum = 10,000
Else
Sum = TextBox1.Text + TextBox2.Text
但是,(我想)因为在我把数字加在一起之前,我要把它们转换成货币,所以无论什么情况,它总是返回10,000。
我知道我可能错过了一些很明显的东西。如果你需要澄清,就问吧!谢谢你的帮助。
下面的代码从一些透视表数据生成一个图。在我将索引中的值'<-24‘重命名为'less’之后,透视表数据如下图所示:Pivot Table Data 并不是所有的代码都包含在... import pandas as pd
import numpy as np
import os
import matplotlib.pyplot as plt
import openpyxl
from datetime import datetime, timedelta
from openpyxl import load_workbook
...
# Creating the pivo
我在python3中重写了python2代码中的一个函数
def abc(self, id):
if not isinstance(id, int):
id = int(id)
mask = self.programs['ID'] == id
assert sum(mask) > 0
name = self.programs[mask]['name'].values[0]
这里的"id“是一个panda序列,它的索引是字符串,列是int,如下所示
data = np.array(['1
程序1
int sum = 30;
double avg = sum / 4; // result is 7.0, not 7.5 !!!
VS.
程序2
int sum= 30
double avg =sum/4.0 // Prints lns 7.5
这是因为程序1中的'4‘是一个文字整数吗?因此,30/4将给我7。然而,由于这个数据类型是一个双,我们需要添加一个.0结束。所以“7.0”
程序2有4.0,它充当文字的双重角色。int/double总是会给出双倍,因为它更精确。所以我们得到了7.5。我不明白双重数据类型对结果有什么影响。它实际上不需要做任何事情,因为双数据类
我想使用下面的python脚本对CSV文件中的一列进行求和。但是,我得到了以下错误。
invalid literal for int() with base 10: '7.3'
我的脚本出了什么问题?
import csv
cr = csv.reader(open("companylist.csv","rb"))
cr.next() # to skip the header
total = 0
for row in cr:
total += int(row[2])
# possibly do other things wit
我有一个相对较大的程序,并不是所有的程序都与我的问题有关,但这一点让我很为难。下面是我的int main:
int main ()
{
int caseNumber ;
string clientName, clientEmail, subject, path, fileName, firstTime ;
//creates string for path of files created and where the Python project is stored (with the .exe)
path = _pgmptr ;
fil