我正在尝试让我了解每次计算hori_dist时的增量增加。相反,它只是向我显示最终结果加在一起。由于我的范围是5,如何获取5个值?
通过Google搜索视频,在线搜索,并尝试了多种方式
#include <boost/graph/adjacency_list.hpp>
#include <iostream>
struct PropertyObj {
std::string something;
};
template <class Graph, class T = PropertyObj>
class Wrapper {
public:
using vertex_descriptor = typename Graph::vertex_descriptor;
T& get_property_obj(vertex_descriptor v) {
if (!descriptor_looks_valid(v))
throw std::range_error("get_property_obj");
return g[v];
}
Wrapper(Graph& g) : g(g){}
private:
bool descriptor_looks_valid(vertex_descriptor v) const {
if constexpr(std::is_integral_v<vertex_descriptor>) {
return v>=0 && v < num_vertices(g);
} else {
auto vds = vertices(g);
return std::count(vds.first, vds.second, v);
}
}
Graph& g;
};
template <typename vertexS> void doTest() {
using Graph = boost::adjacency_list<
boost::vecS,
vertexS,
boost::directedS,
PropertyObj>;
Graph g;
auto v1 = add_vertex({"one"}, g);
auto v2 = add_vertex({"two"}, g);
auto v3 = add_vertex({"three"}, g);
auto v4 = add_vertex({"four"}, g);
auto v5 = add_vertex({"five"}, g);
boost::ignore_unused_variable_warning(v1);
boost::ignore_unused_variable_warning(v2);
boost::ignore_unused_variable_warning(v3);
boost::ignore_unused_variable_warning(v4);
boost::ignore_unused_variable_warning(v5);
Wrapper w = g;
std::cout << w.get_property_obj(v3).something << std::endl;
// but this is confounding, and only accidentally "works" for vecS:
remove_vertex(v1, g);
std::cout << w.get_property_obj(v3).something << std::endl;
try {
// this looks "valid" with vecS, but should throw for listS
//
// of course, like with v3 the descriptor was already invalidated both cases
std::cout << w.get_property_obj(v1).something << std::endl;
} catch(std::range_error const& re) {
std::cout << "(range_error caught: " << re.what() << "\n";
}
}
int main() {
std::cout << "Testing with vecS:\n";
doTest<boost::vecS>();
std::cout << "\nTesting with listS:\n";
doTest<boost::listS>();
std::cout << "\nTesting with setS:\n";
doTest<boost::setS>();
}
答案 0 :(得分:1)
要定义范围,许多语言都使用花括号 {} 。但是,Python使用缩进。
因此,如果您希望某件东西被打印5次,则可以将其包含在for循环中。可能会对您有帮助。
Vx0 = 18
Vy0 = 18
V = 18
Theta = 45
y0 = 1.8
x0 = 0
p = 1.2 #given density
C = 0.3 #Drag coefficient
dt = 0.2 #Time increment
def x_direction (x0, Vx0, dt):
"""Calculate the distance moved in x axis"""
new_dist = 0
for hori_dist in range(5):
hori_dist = x0 + Vx0*dt
new_dist = new_dist + hori_dist
print ("[LOOP] Distance being moved is", hori_dist) #The extra print
print ("[LOOP] New distance is", new_dist) #Another extra print
return new_dist
new_dist = x_direction(x0, Vx0, dt)
print ("Distanced moved in x direction is :", new_dist)
答案 1 :(得分:-1)
只需将print
语句放入循环中,就像这样
hori_dist = x0 + Vx0*dt
print (hori_dist)
此外,您不能像在这里x_direction(x0, Vx0, dt)
那样调用函数。您应该传递值而不是变量x_direction(0, 18, 0.2)