我告诉我第1行和第5行(新的调试/编程,不确定是否有帮助)
def hi():
print 'hi'
def loop(f, n): #f repeats n times
if n<=0:
return
else:
f()
loop(f, n-1)
loop(hi(), 5)
hi
TypeError: 'NoneType' object is not callable
Run Code Online (Sandbox Code Playgroud)
为什么它会给我这个错误?
我收到错误"错误:第6行的'{'token'之前的预期unqualified-id.
我不知道出了什么问题.
#include <iostream>
using namespace std;
class WordGame;
{
public:
void setWord( string word )
{
theWord = word;
}
string getWord()
{
return theWord;
}
void displayWord()
{
cout << "Your word is " << getWord() << endl;
}
private:
string theWord;
}
int main()
{
string aWord;
WordGame theGame;
cin >> aWord;
theGame.setWord(aWord);
theGame.displaymessage();
}
Run Code Online (Sandbox Code Playgroud) 当我运行它时,在main()中cout打印5.395.但断言说它失败了!! 这真是令人难以置信,为什么会发生这种情况?
#include <iostream>
#include <cassert>
using namespace std;
const float A = 1.6;
const float C = 1.55;
const float G = 2.2;
const float T = 1.23;
char empty[18];
int arraySize;
void copyArray(char sourceArray[], char targetArray[], int size) {
for(int i=0;i<size;i++) {
targetArray[i] = sourceArray[i];
}
}
double getAvgDensity(char aminoAcid) {
char aminoUpper = toupper(aminoAcid);
aminoToArray(aminoUpper);
double counter = 0;
int codonTotal = arraySize / 3.0;
if (arraySize == 0)
return 0;
else
{
for (int i = …Run Code Online (Sandbox Code Playgroud) 我只是想知道为什么这不起作用(我试图从一本书中给小鸭子起名:Jack,Kack,Lack,Mack,Nack,Ouack,Pack,Quack)注意:Quack和Ouack有一个U
prefixes = 'JKLMNOPQ'
suffix = 'ack'
for letter in prefixes:
if letter != 'O' or 'Q': #I know this doesn't work, need to know alternative
print letter + suffix
else:
print letter + 'u' + suffix
Run Code Online (Sandbox Code Playgroud) 我需要有关参数的帮助.这两个函数定义是否对print_twice执行完全相同的操作?
def print_twice(lol):
print lol
print lol
def print_twice(michael):
print michael
print michael
Run Code Online (Sandbox Code Playgroud)
如果是,那么我猜测用于参数的单词无关紧要,对吗?
这是为了模拟被抛出的2个6面骰子.但是当我投入10次投掷时,它会随机投掷任意数量(4,5,6等).我错过了什么吗?
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
int throwDice() // returns random number ranged 2-12
{
int x = (rand() % 11) + 2;
return x;
}
int main()
{
srand (time(NULL));
int y;
cout << "Roll dice how many times?" << endl;
cin >> y;
int a2 = 0;
int a3 = 0;
int a4 = 0;
int a5 = 0;
int a6 = 0;
int a7 = 0;
int a8 = 0;
int …Run Code Online (Sandbox Code Playgroud) 这是一个链表,我正在尝试重载==,它给了我错误:错误:将'const Linked_List传递为'ItemType Linked_List :: Element(int)[with ItemType = int]'的'this'参数丢弃限定符.
我必须检查两个链表是否具有相同数量的元素,以及每个元素是否成对相等.
错误指向==的实现部分这里是摘录:PS:element(i)返回链表的i位置的值
template <typename ItemType>
bool Linked_List <ItemType>::operator == (const Linked_List &eq)const {
if (eq.Items != Items){
return false;
}
if (eq.Items == 0){
return true;
}
for (int i = 0; i < eq.Items; i++){
if (eq.Element(i) != Element(i)){ //error points here
return false;
}
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
这是我可能相关的其余代码.我没有发布我的所有代码,因为一切正常,包括element(),只有重载不起作用.
//.h
template <typename ItemType>
class Node
{
public:
ItemType Data;
Node <ItemType> *next;
};
template <typename ItemType>
class Linked_List
{ …Run Code Online (Sandbox Code Playgroud) 我正在读一本c ++书,我有几个问题.
1)在此示例中,void仅用于声明返回类型吗?
2)如果void导致它不将数据返回给调用函数,为什么它仍然显示消息"欢迎来到成绩簿!"?
3)创建一个简单的函数而不是创建一个对象是不是更容易?
#include <iostream>
using namespace std;
class GradeBook
{
public:
void displayMessage()
{
cout << "Welcome to the Grade Book!" << endl;
}
};
int main()
{
GradeBook myGradeBook;
myGradeBook.displayMessage();
}
Run Code Online (Sandbox Code Playgroud) 调试还不是很好,但是我遇到了一些错误.一些预期'('')'和';' 另外'else'没有先前的'if',在cout中与'operator >>不匹配
我知道这很容易,但仍然试图让我的脚踏上门.谢谢 :)
#include <iostream>
#include <cstdlib>
using namespace std;
int main() // guess number game
{
int x;
cout >> "Please enter a number\n";
getline(cin x);
int y = rand();
while x != y
{
if x < y;
cout >> "Go higher";
else;
cout >> "Go lower";
}
}
Run Code Online (Sandbox Code Playgroud) 结果打印出"c"3次,任何人都知道它为什么总是满足第一个条件?
#include <iostream>
using namespace std;
char x(char y)
{
if (y == 'a' || 'b')
{
return 'c';
}
else if (y == 'c' || 'd')
{
return 'e';
}
else
{
return 'g';
}
}
int main()
{
cout << x('a') << endl;
cout << x('c') << endl;
cout << x('p') << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)