如何在终端中运行一行Python命令?

我想在终端中获取Python函数的结果。

我尝试运行命令:

#include <iostream>
#include <conio.h>
using namespace std;

int main() {
  int score;
  string eAnswer1,eAnswer2,aEasy1,aEasy2;

  aEasy1 = "source code";
  aEasy2 = "language translator";

  cout << "1st Question: It is a well-written set of instructions and statements to develop a program. Answer: ";
  cin.ignore();
  getline(cin,eAnswer1);
  if (eAnswer1 == aEasy1) {
    score = score + 1;
  }

  cout << "2nd Question: Source code must be translated to machine language using a? Answer: ";
  cin.ignore();
  getline(cin,eAnswer2);
  if (eAnswer2 == aEasy2) {
    score = score + 1;
  }

  cout << score << endl;
  return 0;
}

我希望看到的输出类似于:$ python3 -m uuid uuid.uuid4().hex

不幸的是我得到了错误:

'78cbf0fadaa34ff7ac3f7b965965e207'
sjmissys 回答:如何在终端中运行一行Python命令?

你很近。

  • 运行单个命令的标志是-c,而不是-m
  • 您还需要import uuid才能使用它。
  • 您还需要使用print()来实际看到一些输出。
  • 最后,整个传递的命令必须用引号引起来。

$ python3 -c "import uuid; print(uuid.uuid4().hex)"
本文链接:https://www.f2er.com/3152031.html

大家都在问