对我来说,我有一个不寻常的功课问题:
不要修改主函数,但更改它作为输出打印的程序:
- 星期一
- 是第一天
- 一周中的
并且继承了给定的代码:
int main()
{
cout << "is the first day\n";
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激 :)
在制定了如何检查猜测是否正确的功能后,我很难说出我想要的东西.
ntries = 0
while ntries < 10:
ntries +=1
if test_guess(code,guess)==True:
print 'You win! You guessed the code in',ntries,'tries.'
elif test_guess(code,guess)==False:
guess = str(raw_input('Your guess: '))
print 'You lose!'
Run Code Online (Sandbox Code Playgroud)
问题是,当玩家成功猜测代码时,例如8次尝试,打印的结果是:
> You win! You guessed the code in 8 tries.
> You win! You guessed the code in 9 tries.
> You win! You guessed the code in 10 tries.
> You lose!
Run Code Online (Sandbox Code Playgroud)
我意识到这是因为while循环表明当ntries仍然小于10时循环继续运行.我如何才能使它只打印赢得的尝试次数并停在那里?
我试图找出我的错误在下面的代码中,这是关于猜一个字.
如果我输入缺失的单词,"ghost"
它应该结束游戏,但它会继续要求它.
我的错误在哪里?
number = 0
missword = "ghost"
while number < 3:
guess = input("What is the missing word? ")
if guess != "ghost":
print("Try again")
number = number + 1
elif guess == missword:
print("Game Over")
else:
print("Game over")
Run Code Online (Sandbox Code Playgroud) 这是官方python文档中给出的用于打印Fibonacci系列的代码.
我不明白为什么这个代码运行到无穷大,因为while循环条件没有问题.
def fib(n):
a, b = 0, 1
while a < n:
print a,
a, b = b, a + b
number = raw_input("What's the number you want to get Fibonacci series up to?")
fib(number)
Run Code Online (Sandbox Code Playgroud) 从Python的源代码来看open
,我认为open
这只是一个普通的函数.
为什么我们可以像下面这样使用它?
with open('what_are_context_managers.txt', 'r') as infile:
for line in infile:
print('> {}'.format(line))
Run Code Online (Sandbox Code Playgroud)
因为既不是实现__enter__
也不是__exit__
,也不使用contextlib.contextmanager
装饰器.
另一个"结果太大" - c ++套接字的问题并没有多大帮助.我有一个服务器代码,但每次运行它时,我会得到一个"结果太大" - 对于listen() - 函数的错误.希望,你可以帮忙!
SOCKET sd;
SOCKET sd2;
char serve_1_clien_0_intern;
struct sockaddr_in server;
struct sockaddr_in client;
void udp_init(unsigned short port_number, int a1, int a2, int a3, int a4, char serve_1_clien_0 ){
WSADATA w;
serve_1_clien_0_intern = serve_1_clien_0;
if (WSAStartup(0x0101, &w) != 0){
fprintf(stderr, "Could not open Windows connection.\n");
return;
exit(0);
}
sd = socket(AF_INET, SOCK_DGRAM, 0);
if (sd == INVALID_SOCKET){
fprintf(stderr, "Could not create socket.\n");
WSACleanup();
return;
exit(0);
}
memset((void *)&server, '\0', sizeof(struct sockaddr_in));
server.sin_family = AF_INET; …
Run Code Online (Sandbox Code Playgroud) 我试图在我的正则表达式中结合 if else ,基本上如果字符串中存在某些模式,则捕获一种模式,如果没有,则捕获另一种模式。
字符串是:' https://www.searchpage.com/searchcompany.aspx?companyId=41490234&page=0&leftlink=true ”,我想提取'?
因此,如果 '?' 在字符串中检测到,正则表达式应该捕获 '?' 之后的所有内容。标记; 如果没有,则从头开始捕获。
我用过:'(.*\?.*)?(\?.*&.*)|(^&.*)'
但它没有用...
有什么建议吗?
谢谢!
我下面有一些代码,choice
只接受整数输入,但如果输入不是整数,则输出一些特殊的代码.但是,下面的代码来处理这个问题似乎有点冗长.无论如何要解决它?
from sys import exit
def gold_room():
print "This room is full of gold. How much do you take?"
choice = raw_input("> ")
if "0" in choice or "1" in choice or "2" in choice or "3" in choice or "4" in choice or "5" in choice or "6" in choice or "7" in choice or "8" in choice or "9" in choice:
how_much = int(choice)
else:
dead("Man, learn to type a number.")
if how_much < 50:
print …
Run Code Online (Sandbox Code Playgroud) 如果跟随代码示例之间存在差异?
ClassTwo ClassTwo::getOwnOne(){
return *this;
}
ClassTwo& ClassTwo::getOwnTwo(){
return *this;
}
Run Code Online (Sandbox Code Playgroud)
如果跟随代码示例之间存在差异?是一样的吗?
我有一个文本文件,如下面的read.txt:
1.0 2.0 3.0 4.0
2.0 3.0 4.0 6
5.0 7 1.0 5.0
Run Code Online (Sandbox Code Playgroud)
calc.cpp:
void main()
{
FILE *fp;
fp=fopen("read.txt","r");
double *read_feature = new double*[3];
for(i = 0; i<3; i++)
read_feature[i] = new double[3];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
fscanf(fp,"%lf",&read_feature[i][j]);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想计算我的文本文件中的所有数字(read.txt).读取文本文件由浮点数和整数组成.上述文件的答案是12.
如何动态地使用c ++计算文件中的浮点数?这意味着不给出i和j的值.我需要程序会自动计算i和j的总数.