我正在尝试一段简单的代码,得到别人的名字和年龄,让他/她知道他们什么时候年满21岁......不考虑否定和所有这些,只是随机的.
我一直收到这个'int' object is not subscriptable错误.
name1 = raw_input("What's your name? ")
age1 = raw_input ("how old are you? ")
x = 0
int([x[age1]])
twentyone = 21 - x
print "Hi, " + name1+ " you will be 21 in: " + twentyone + " years."
Run Code Online (Sandbox Code Playgroud) 我做了一个简单的程序,允许用户选择一些骰子然后猜测结果...我之前发布了这个代码,但是错误的问题所以它被删除了...现在我不能有任何错误甚至警告代码,但由于某种原因,这个警告保持弹出,我不知道如何解决它... "警告C4244:'参数':从'time_t'转换为'unsigned int',可能丢失数据"
#include <iostream>
#include <string>
#include <cstdlib>
#include <time.h>
using namespace std;
int choice, dice, random;
int main(){
string decision;
srand ( time(NULL) );
while(decision != "no" || decision != "No")
{
std::cout << "how many dice would you like to use? ";
std::cin >> dice;
std::cout << "guess what number was thrown: ";
std::cin >> choice;
for(int i=0; i<dice;i++){
random = rand() % 6 + 1;
}
if( choice == random){
std::cout << "Congratulations, you got it …Run Code Online (Sandbox Code Playgroud) 我一直试图通过以下方法查询视频:
function search() {
var request = gapi.client.youtube.search.list({
part: 'snippet',
channelId: 'IRRELEVANT',
order: 'date',
maxResults: '25'
});
request.execute(function(response){
YoutubeResponse(response);
});
Run Code Online (Sandbox Code Playgroud)
虽然文档告诉我statistics每个视频都有一部分,但是snippet我认为__proto__这意味着某个地方出现了错误?或API改变了吗?基本上我需要那些最近25个视频的视图数...
我试过换part: 'snippet'到part: 'statistics'但是又回来了code: -32602......
谢谢您的帮助,
干杯!
编辑:显然search.list没有"统计",而是我需要单独搜索每个视频......当使用谷歌"尝试"功能时(https://developers.google.com/youtube/v3)/docs/videos/list#try-it)当你在底部的"Fields"部分询问统计信息时,它没有做任何事情......所以我非常困惑,我怎么能得到查看所有25个视频的计数和长度(如果单独或全部一次 - 最好 - )
我试图理解如何stringstream工作,以便能够识别和转换作为字符串输入的可能数字......由于某种原因,我写的这段小代码尝试和理解stringstream是令人烦恼的一些错误...
#include <iostream>
#include <string>
using namespace std;
int str2int (const string &str) {
std::stringstream ss(str);
int num;
if((ss >> num).fail())
{
num = 0;
return num;
}
return num;
}
int main(){
int test;
int t = 0;
std::string input;
while (t !=1){
std::cout << "input: ";
std::cin >> input;
test = str2int(input);
if(test == 0){
std::cout << "Not a number...";
}else
std::cout << test << "\n";
std::cin >> t;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误: …
我正在尝试做一个带日期的简单页面(输入类型为TEXT),一旦输入日期,另一个字段将在输入中添加7天并在文本输入中显示日期(+7天).我对jQuery的了解有限,所以我可能有一个小bug ...
<html>
<head>
<title>Date Plus 7 Days</title>
<script type="text/javascript">
$(document).ready(function(){
function DateFromString(str){
str = str.split(/\D+/);
str = new Date(str[2],str[0]-1,(parseInt(str[1])+7));
return MMDDYYYY(str);
}
function MMDDYYYY(str) {
var ndateArr = str.toString().split(' ');
var Months = 'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec';
return (parseInt(Months.indexOf(ndateArr[1])/4)+1)+'/'+ndateArr[2]+'/'+ndateArr[3];
}
function Add7Days() {
var date = $('#start_date').val();
var ndate = DateFromString(date);
return ndate;
}
$('#start_date').change(function(){
$('#end_date') = Add7Days();
})
});
</script>
</head>
<body>
Start Date
<input type="text" id="start_date" value=''>
<br>
End …Run Code Online (Sandbox Code Playgroud) 我一直在试图弄清楚如何将数组添加到结构中……例如,整数结构看起来像这样:
struct test{
int a;
int b;
int c;
} test = {0,1,2};
Run Code Online (Sandbox Code Playgroud)
但如果我想要一个数组,例如:
struct test{
int a;
int b;
int c;
int deck[52];
} test;
Run Code Online (Sandbox Code Playgroud)
这是可行的吗?一副(纸牌)的初始化发生在不同的函数中。当我这样做时,我没有收到错误,struct但是当我尝试使用它时我得到了它......例如,如果我这样做,test.deck[i] = 1;它会给我这个错误:
Error C2143 Syntax Error missing ';' before '.'
Run Code Online (Sandbox Code Playgroud)
如果我要使用a,我可以写test.a = 1;
任何人都可以编写一个简单的结构,其中的变量是一个数组,然后将其用于简单的命令吗?
我被一个int转换为字符串,扭转字符串,使该字符串返回一个int来比较它(可能比较字符串也没有问题)制作一个简单的回文探测器,但由于某些原因,反向串保持原值并将新的值添加到行而不是替换它们......为什么?
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main(){
string tempreverse;
string temp;
stringstream out;
int tempnumber, tempnumber2;
int palindrome = 0;
for(int i = 100; i < 111; i++){
for(int j = 100; j < 111; j++){
tempnumber = i * j;
out << tempnumber;
temp = out.str();
tempreverse = string (temp.rbegin(), temp.rend());
tempnumber2 = atoi(tempreverse.c_str());
if (tempnumber == tempnumber2){
palindrome = tempnumber;
}
}
}
cout << palindrome << "\n";
cout << "Press ENTER to …Run Code Online (Sandbox Code Playgroud) 我面对这个问题进行C++考试,而且我认为我知道答案,这个问题让我很困惑,我宁愿每个人都说"第二意见"......哈哈.
a)文字
"Hello, World!"是C风格的字符串.它的类型是什么?b)鉴于C风格字符串的类型和类型
std::string不同,如何在std::string hello("Hello,World!");没有错误/警告的情况下编译该行(假设<string>已包含标题)?
根据我的理解,C-Style字符串是类型(a)字符数组,而std::string类型为字符串...和(b)它编译的原因是在C++中整个转换为字符,所有这些都是在"引擎盖"由类型字符串而不是"在代码中".
我对么?
谢谢!