入门级python。摆脱逗号

时间:2018-04-27 01:07:23

标签: python

这是我在Stackoverflow上的第一篇文章。刚开始学习编码。

我试图让这段代码发挥作用。确实如此,但我不喜欢结果。 例如,如果我输入" 25",我会得到这样的结果:

25,50,75,

我想要的是在最后摆脱逗号。有人可以给我一个提示吗?

谢谢!

 <?php

namespace App\DataFixtures;

use App\Entity\Category;
use App\Entity\User;
use App\Entity\Post;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityManager;
use Faker\Factory;
use Faker\Generator;

class AppFixtures extends Fixture
{
    private $faker;

    private $categories;

    public function load(ObjectManager $manager)
    {
        $this->faker = Factory::create();
        $this->addUsers($manager);
        $this->addCategories($manager);
        $this->addPosts($manager);

        $manager->flush();
    }

    private function addUsers(EntityManager $em)
    {
        for ($i = 1; $i <= 10; $i++) {
            $user = new User();
            $firstname = $this->faker->firstName;
            $lastname = $this->faker->lastName;
            $user->setFirstName($firstname);
            $user->setLastName($lastname);
            $user->setEmail($firstname.'.'.$lastname.'@gmail.com');
            $user->setRoles(['ROLE_USER']);
            $em->persist($user);
            $this->addPosts($user);
        }
    }

    private function addCategories(EntityManager $em)
    {
        $categoryHome = new Category();
        $categoryHome->setName('Home');
        $em->persist($categoryHome);

        $this->categories[] = $categoryHome;

        $categoryWork = new Category();
        $categoryWork->setName('Work');
        $em->persist($categoryWork);

        $this->categories[] = $categoryWork;
    }

    public function addPosts(EntityManager $em, User $user){
        for ($i = 1; $i <= 10; $i++) {
            $post = new Post();
            $post->setUser($user);


            $post->setCaterory($this->categories[rand(0, count($this->categories))]);
            $em->persist($post);
        }
    }
}

0 个答案:

没有答案