Seite 1 von 1
CodeTest
Verfasst: Sonntag 7. Juni 2020, 12:56
von Ali97
Hi und zwar bin ich grad dabei die programmier sprachen Python zu erkennen und zwar komme ich grad an eine aufgabe nicht weiter ich hoffe ihr könnt mir helfen
Code: Alles auswählen
def city_country(city, country, population=0):
"""Retrun it back."""
if population:
c_c = city.title() + ' ' + ' ' + country.title() + ' ' + str(population)
else:
c_c = city.title() + ' ' + country.title()
return c_c
/code]
[code]import unittest
from city_functions import city_country
class NameTestCase(unittest.TestCase):
"""Tests for city country."""
def test_city_country(self):
"""Work the string city country work"""
beirut_lebanon = city_country('beirut', 'lebanon')
self.assertEqual(beirut_lebanon, 'Beirut Lebanon')
def test_city_country_population(self):
"""Work the city, country, population."""
beirut_lebanon_population = city_country(
'new-york', 'america', 500000
)
self.assertEqual(beirut_lebanon_population, 'New-York America', 500000)
unittest.main()
/code]
Immer wieder wen ich versuche den code auszuführen gibt es mir einen Fehler aus dass etwas Falsch ist an der code Zeile selfessertEqual
aber wen ich bei der Oberligen Zeile die 500000 auslasse und unten stehen lasse bei selfEqual funktioniert der code
Re: CodeTest
Verfasst: Sonntag 7. Juni 2020, 14:03
von Ali97
Ali97 hat geschrieben: Sonntag 7. Juni 2020, 12:56
Hi und zwar bin ich grad dabei die programmier sprachen Python zu erlernen und zwar komme ich grad bei einer aufgabe nicht weiter ich hoffe ihr könnt mir helfen
Code: Alles auswählen
def city_country(city, country, population=0):
"""Retrun it back."""
if population:
c_c = city.title() + ' ' + ' ' + country.title() + ' ' + str(population)
else:
c_c = city.title() + ' ' + country.title()
return c_c
/code]
[code]import unittest
from city_functions import city_country
class NameTestCase(unittest.TestCase):
"""Tests for city country."""
def test_city_country(self):
"""Work the string city country work"""
beirut_lebanon = city_country('beirut', 'lebanon')
self.assertEqual(beirut_lebanon, 'Beirut Lebanon')
def test_city_country_population(self):
"""Work the city, country, population."""
beirut_lebanon_population = city_country(
'new-york', 'america', 500000
)
self.assertEqual(beirut_lebanon_population, 'New-York America', 500000)
unittest.main()
/code]
Immer wieder wen ich versuche den code auszuführen gibt es mir einen Fehler aus dass etwas Falsch ist an der code Zeile selfessertEqual
aber wen ich bei der Oberligen Zeile die 500000 auslasse und unten stehen lasse bei selfEqual funktioniert der code
[/quote]
Re: CodeTest
Verfasst: Sonntag 7. Juni 2020, 14:19
von Sirius3
Wichtig beim Programmieren sind aussagekräftige Variablennamen. Was soll c_c denn bedeuten?
Strings setzt man nicht mit + zusammen, sondern nutzt FormatStrings:
Code: Alles auswählen
def city_country(city, country, population=0):
if population:
return f"{city.title()} {country.title()} {population}"
else:
return f"{city.title()} {country.title()}"
Das `functions` im Modulnamen `city_functions` ist wenig hilfreich, denn was soll passieren, wenn nicht nur Funktionen im Modul definiert werden?
Die API für Unittests von `unittest` ist sehr an Java angelehnt und wenig Python-artig. Besser ist es, statt dessen py.test zu installieren und zu nutzen.
Und der Fehler kommt ja auch zurecht, weil das Ergebnis nicht das ist, was Du da testest.
Code: Alles auswählen
from city_functions import city_country
def test_city_country():
"""Work the string city country work"""
beirut_lebanon = city_country('beirut', 'lebanon')
assert beirut_lebanon == 'Beirut Lebanon'
def test_city_country_population():
"""Work the city, country, population."""
beirut_lebanon_population = city_country(
'new-york', 'america', 500000
)
assert beirut_lebanon_population == 'New-York America 500000'
Re: CodeTest
Verfasst: Montag 8. Juni 2020, 16:12
von Ali97
Ok danke dass ist gut zu wissen
Hab den code geändert jetzt gibt er mir alles richtig aus
hier nochmal der neue code
Code: Alles auswählen
def city_country(city, country, population=0):
"""Retrun it back."""
output_string = city.title() + ', ' + country.title()
if population:
output_string += ' population ' + str(population)
return output_string
/code]
[code]import unittest
from city_functions import city_country
class NameTestCase(unittest.TestCase):
"""Tests for city country."""
def test_city_country(self):
"""Work the string city country work"""
beirut_lebanon = city_country('beirut', 'lebanon')
self.assertEqual(beirut_lebanon, 'Beirut, Lebanon')
def test_city_country_population(self):
"""Work the city, country, population."""
beirut_lebanon_population = city_country('new-york', 'america', population=5000000)
self.assertEqual(beirut_lebanon_population, 'New-York, America population 5000000')
unittest.main()
/code]
Re: CodeTest
Verfasst: Montag 8. Juni 2020, 17:09
von Sirius3
@Ali97: und welche der Anmerkungen von mir hast Du nun umgesetzt?
Re: CodeTest
Verfasst: Freitag 12. Juni 2020, 11:17
von Ali97
Ehrlich gesagt keiner von den da ich noch in dem buch von Python Crash course anders
coden
Aber ich danke trotzdem fürs spätere

)