我正在寻找我的特定代码的错误答案.我搜查了其他人,他们仍然很混乱.
我不确定为什么会这样.
以下是错误引用的代码部分,后跟错误.
def processScores( file, score):
#opens file using with method, reads each line with a for loop. If content in line
#agrees with parameters in if statements, executes code in if statment. Otherwise, ignores line
with open(file,'r') as f:
for line in f: #starts for loop for all if statements
if line[0].isdigit:
start = int(line)
score.initialScore(start) #checks if first line is a number if it is adds it to intial score
Run Code Online (Sandbox Code Playgroud)
我收到的错误消息:
Traceback (most recent call last):
File …Run Code Online (Sandbox Code Playgroud) 如何验证字符串中的每个字符是否为0-9位?例如:1343151234234有效但2342343ABC34234无效.谢谢.
我有一个textBox在我的程序包含string有以满足一些要求.我问这个问题是找出满足这些要求的最佳方法.
这string不可能NullOrEmpty,它必须完全由整数组成.该string还可以包含空格,这是我的症结所在,因为空间是不是整数.
这就是我正在使用的(我知道它目前可能有点多余):
//I test the string whenever the textBox loses focus
private void messageBox_LostFocus(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(TextBox.Text))
ButtonEnabled = true;
else if (Regex.IsMatch(TextBox.Text, @"^\d+$") == false)
{
//I think my problem is here, the second part of the if statement doesn't
//really seem to work because it accepts characters if there is a space
//in the string.
if (TextBox.Text.Contains(" ") && !Regex.IsMatch(TextBox.Text, @"^\d+$"))
ButtonEnabled = true; …Run Code Online (Sandbox Code Playgroud) 项目概述:
我正在创建一个多表单应用程序,它由两个表单和一个父类组成.两者Forms都有一系列验证功能,例如isLetter() isNumber()和isValidEmail().使用isNumber()函数时出现了我的问题.
public static bool numValidation(string strNum)
{
if (!string.IsNullOrWhiteSpace(strNum))
{
int temp;
if (int.TryParse(strNum, out temp))
{
Console.WriteLine("Phone Number is a valid input: " + temp);
return true;
}
else
{ Console.WriteLine(temp + "Is not Valid input!!"); }
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
乍一看它工作正常,但一旦我试图打破它,我意识到当输入一个实际的电话号码时,我得到一个错误,说这个数字太高了.任何想法如何绕过这个约束?因为我需要这个验证的唯一原因是电话号码,传真等.我只需要这个功能来接受非常大的数字,如电话号码
如何检查字符串是否为数字.我正在验证手机号码,它应该有10位数字,并且只能采用数字格式.
string str="9848768447"
if(str.Length==10 && Here I need condition to check string is number or not)
{
//Code goes here
}
Run Code Online (Sandbox Code Playgroud)
我是编程新手.请帮我