Lese wieviele 1 Dokument enthält?

  • Also wenn ich jetzt das Dokument

    Code
    01010101

    habe will ich als Output haben das es 4 nullen und 5 einsen gibt. Wie macht man das? Und ausserdem wie included man eigentlich eine API in Python? Also ich habe jetzt diesen Code hier gefunden


    Ich weiss wie ich es aufrufe nur wie importe ich es? Also diesen Text als Datei speichern und dann import Dateiname.py ?

  • Code
    framp@majestix:~$ python
    Python 2.7.3 (default, Dec 18 2014, 19:10:20) 
    [GCC 4.6.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> b="10110"
    >>> print b.count("1")
    3
    >>> print b.count("0")
    2
    >>>
  • Ich weiss wie ich es aufrufe nur wie importe ich es?


    Wenn du diese Datei aufrufst passiert genau gar nichts ;)

    Quote

    Also diesen Text als Datei speichern und dann import Dateiname.py ?


    Nein - ohne .py am Ende

    Und das solltest du dann auch mal lesen:
    * https://docs.python.org/3/reference/import.html
    bzw.
    * https://docs.python.org/2/tutorial/modules.html

    Gruß, noisefloor

  • Code
    framp@majestix:~$ python
    Python 2.7.3 (default, Dec 18 2014, 19:10:20) 
    [GCC 4.6.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> b="10110"
    >>> print b.count("1")
    3
    >>> print b.count("0")
    2
    >>>

    Ne das geht nicht
    er sagt invalid syntax

  • Welche Version verwendest Du ?

    Framp's Beispiel zielt auf Python 2 ab.

    In Python 3 muss die print-Anweisung in Klammern gesetzt werden:

    [code=php]print(b.count("1"))[/php]

    Mfg,

    sls

    Edited once, last by CrimsonMoon28021 (June 7, 2015 at 2:52 PM).

  • Klammern ?? Da war doch noch was .... ;):D *Insider*

    ;) Gruß Outi :D

    Mein Zeug

    Pis: 2x Pi B, 1x Pi B+, 1x Pi 2 B in Rente / 2x Pi 3 B (Tests / Repetier Server) / 2x Pi Zero 1.2 / 2x Pi Zero 1.3 / 2x Pi Zero W 1.1 / 1x Pi Zero 2 (BW+CUPS/SANE) /
    1x Pi 3 B+ (Tests) / 1x Pi 4 B 4GB (HomeAssistant) / Pi 400 (BW) / 1x Pi 5 8GB (BW) / 2x Pico / 2x Pico W / 2x Pico 2 / 2x Pico 2 W
    HATs: Sense HAT / HM-MOD-RPI-PCB / RPI-RF-MOD / PiFi DAC+ V2.0 / TV HAT / Pi 5 Kühler HAT / Pimoroni NVMe BASE / M.2 HAT+
    Cams: orig. Raspberry Pi Camera Module V1 & V3 / PS3 Eye

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!