我正在制作一个IF语句,如果插入的数字以零结尾,我想要发生一些事情.以下不起作用:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int year;
printf("Skriv in ditt födelseår\n");
scanf("%d", &year); printf("Du är %d", 2013 - year); printf(" år gammal");
if ( 2013 - year.endsWith('0') ) {
printf("Grattis, du fyller jämnt iår!\n");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
因此,如果2003年的结果 - 年(用户输入的年份)以零结尾,我想打印一些东西.我如何使其工作?
对于作业,我必须从以"W"和"Z"开头并以"n"和"t"结尾的文本中打印行(所以Wn,Wt,Zn,Zt组合).我现在有一个代码可行,但似乎有点长,我想知道是否有办法缩短它?
这是代码:
import sys
def main():
for line in sys.stdin:
line = line.rstrip()
if line.startswith("W") and line.endswith("n"):
print(line)
if line.startswith("W") and line.endswith("t"):
print(line)
if line.startswith("Z") and line.endswith("n"):
print(line)
if line.startswith("Z") and line.endswith("t"):
print(line)
main()
Run Code Online (Sandbox Code Playgroud)
正如我所说,它有效,但似乎有点复杂.有关如何缩短的任何提示?
我试过line.startswith("Z","W"),line.endswith("t","n")但我得到一个类型错误(所有切片索引必须是整数或无或有一个__index__method).
我想根据他们的名字选择一些变量来转换它们。变量名都以开头inq和结尾7, 8, 10, 13:15。这对我不起作用...很抱歉,这很明显,但是我无法使其正常工作。我使用了错误的函数,将我的函数和参数放错了,还是其他?
一个可重现的示例:
structure(list(inq1_1 = c(NA, 7, 5, 1, 1, 6, 5, 2, NA, NA), inq1_2 = c(NA,
7, 5, 1, 1, 6, 5, 5, NA, NA), inq1_3 = c(NA, 6, 4, 2, 1, 5, 2,
1, NA, NA), inq1_4 = c(NA, 6, 6, 1, 1, 6, 5, 1, NA, NA), inq1_5 = c(NA,
7, 3, 1, 1, 6, 2, 1, NA, NA), inq1_6 = c(NA, 7, 4, 4, 2, 7, 2,
4, NA, …Run Code Online (Sandbox Code Playgroud) replace_ending 函数用新字符串替换句子中的旧字符串,但前提是句子以旧字符串结尾。如果句子中旧字符串出现多次,则只替换末尾的一个,而不是全部替换。例如,replace_ending("abcabc", "abc", "xyz") 应该返回 abcxyz,而不是 xyzxyz 或 xyzabc。字符串比较区分大小写,因此 replace_ending("abcabc", "ABC", "xyz") 应返回 abcabc(未进行任何更改)。
def replace_ending(sentence, old, new):
# Check if the old string is at the end of the sentence
if ___:
# Using i as the slicing index, combine the part
# of the sentence up to the matched string at the
# end with the new string
i = ___
new_sentence = ___
return new_sentence
# Return the original sentence if there is no match
return sentence …Run Code Online (Sandbox Code Playgroud) 我有以下字符串
http://store.aqa.org.uk/qual/newgcse/pdf/AQA-4695-W-SP.PDF
Run Code Online (Sandbox Code Playgroud)
我想要它,所以如果用户忘记输入http://或.PDF,程序将自动纠正此问题.因此,我尝试了这段代码
if (!str.startsWith("http://")) { // correct forgetting to add 'http://'
str = "http://" + str;
}
System.out.println(str);
if (!str.endsWith("\\Q.PDF\\E")) {
str = str + "\\Q.pdf\\E";
}
Run Code Online (Sandbox Code Playgroud)
但是,即使我输入正确的字符串,http://store.aqa.org.uk/qual/newgcse/pdf/AQA-4695-W-SP.PDF
输出也是如此.
http://store.aqa.org.uk/qual/newgcse/pdf/AQA-4695-W-SP.PDF\Q.pdf\E
Run Code Online (Sandbox Code Playgroud)
为什么?为什么要添加另一个 '.PDF'?
我有一个填充字符串对象的列表.如果字符串对象以a结尾W,我想删除/删除W字符串.此外,在唯一的情况下,字符串等于UW我不想删除W.
我试过这个:
masterAbrevs = []
for subject in masterAbrevs:
if subject.endswith("W"):
subject = subject[:-1]
Run Code Online (Sandbox Code Playgroud)
打印后masterAbrevs,我的代码似乎一无所获.善意的帮助.
我正在使用OpenFile对话框打开一个文件,我想确认该文件是excel格式.
我打开的文件是"C:\ Desktop\Distribution.xls",但我的if语句的两个条件都评估为true.我应该使用另一种方法吗?
DialogResult result = openFileDialog1.ShowDialog();
if (result==DialogResult.OK)
{
file = openFileDialog1.FileName;
file = file.Trim();
if (!file.EndsWith(".xlsx")||!file.EndsWith(".xls"))
{
MessageBox.Show("Incorrect file format. Please save file in an .xls format");
}
else
{
book = application.Workbooks.Open(file);
sheet = (Worksheet)book.Worksheets[1];
range = sheet.get_Range("A1", "A1".ToString());
range.EntireRow.Delete(XlDirection.xlUp);
sheet.Cells[1, 2].EntireColumn.NumberFormat = "@";
book.SaveAs(csvConverstion, XlFileFormat.xlCSV);
book.Close(false, Type.Missing, Type.Missing);
application.Quit();
}
Run Code Online (Sandbox Code Playgroud) 我是python的新手,我必须创建一个验证DNA序列的程序.(DNA序列的背景非常快)为了有效:•字符数可以被3整除•前3个字符是ATG•最后3个字符是TAA,TAG或TGA.
我的问题是在if语句中使用布尔项.
endswith=(DNA.endswith("TAA"))
endswith2=(DNA.endswith("TAG"))
endswith3=(DNA.endswith("TGA"))
if length%3==0 and startswith==true and endswith==true or endswith2==true or endswith3==true:
return ("true")
Run Code Online (Sandbox Code Playgroud)
此代码返回以下错误:未定义全局名称"true"
我如何解决这个问题,而且在最后一个笔记中我真的很抱歉.这个问题的答案可能非常简单,在你的脑海中,一个2岁的孩子可以编码:/我做过研究,但我根本没有运气.所以我感谢你花时间去读我的愚蠢问题.
我想在字符串中查找文本(使用C#),以单词"Column"开头并结束任何数字(例如"100").
总之,我想找到:
Column1
Column100
Column1000
但是没有找到:
Column_1
_Column1
Column1 $
我找不到使用正则表达式的方法.
ends-with ×9
startswith ×4
python ×3
python-3.x ×3
string ×3
c# ×2
c ×1
dplyr ×1
if-statement ×1
java ×1
loops ×1
r ×1
regex ×1
tidyverse ×1