2013-06-17 10 views
65

Używam Pythona, aby ustalić, ile dzieci urodzi się w ciągu 5 lat, jeśli dziecko urodziło się co 7 sekund. Problem jest na mojej ostatniej linii. Jak uzyskać zmienną do działania, gdy drukuję tekst po jej obu stronach?Jak mogę wydrukować zmienną i ciąg znaków w tej samej linii w Pythonie?

Oto mój kod:

currentPop = 312032486 
oneYear = 365 
hours = 24 
minutes = 60 
seconds = 60 

# seconds in a single day 
secondsInDay = hours * minutes * seconds 

# seconds in a year 
secondsInYear = secondsInDay * oneYear 

fiveYears = secondsInYear * 5 

#Seconds in 5 years 
print fiveYears 

# fiveYears in seconds, divided by 7 seconds 
births = fiveYears // 7 

print "If there was a birth every 7 seconds, there would be: " births "births" 

Odpowiedz

114

Zastosowanie , oddzielić ciągi i zmiennych podczas drukowania:

print "If there was a birth every 7 seconds, there would be: ",births,"births" 

, w instrukcji print separtes elementy pojedynczym odstępem:

>>> print "foo","bar","spam" 
foo bar spam 

lub lepiej użyć string formatting:

print "If there was a birth every 7 seconds, there would be: {} births".format(births) 

formatowania String jest znacznie bardziej wydajne i pozwala zrobić kilka innych rzeczy, a także, jak: obicia, wypełnić, wyrównanie, szerokość, zestaw precyzja itp

>>> print "{:d} {:03d} {:>20f}".format(1,2,1.1) 
1 002    1.100000 
    ^^^ 
    0's padded to 2 

Demo:

>>> births = 4 
>>> print "If there was a birth every 7 seconds, there would be: ",births,"births" 
If there was a birth every 7 seconds, there would be: 4 births 

#formatting 
>>> print "If there was a birth every 7 seconds, there would be: {} births".format(births) 
If there was a birth every 7 seconds, there would be: 4 births 
1

można użyć string formatting to zrobić:

print "If there was a birth every 7 seconds, there would be: %d births" % births 

czy można dać print wiele argumentów, a zostanie ona automatycznie rozdzielając je spacją:

print "If there was a birth every 7 seconds, there would be:", births, "births" 
+0

dziękuję za odpowiedź Amber. Czy możesz wyjaśnić, co "d" robi po symbolu%? dzięki –

+2

'% d' oznacza" wartość formatu jako liczbę całkowitą ". Podobnie, '% s' będzie" wartością formatu jako ciągiem ", a'% f' jest "wartością formatu jako liczba zmiennoprzecinkowa". Te i inne są udokumentowane w części podręcznika Pythona, z którym łączyłem się w mojej odpowiedzi. – Amber

+0

Dziękuję bardzo. –

5

można użyć do ŁAŃCUCH_FORMATUJĄCY:

print "There are %d births" % (births,) 

lub w tym prostym przypadku:

print "There are ", births, "births" 
+0

zachowaj ostrożność, jeśli używasz tej drugiej metody, ponieważ jest to krotka, a nie ciąg znaków. – TehTris

28

dwa kolejne

pierwszy

>>>births = str(5) 
>>>print "there are " + births + " births." 
there are 5 births. 

Podczas dodawania ciągi, ich łączenia.

drugi

także (Python 2.6 i nowsze) metoda sznurki format jest chyba standardowy sposób:

>>> births = str(5) 
>>> 
>>> print "there are {} births.".format(births) 
there are 5 births. 

Ta metoda format może być używany z wykazów oraz

>>> format_list = ['five','three'] 
>>> print "there are {} births and {} deaths".format(*format_list) #unpack the list 
there are five births and three deaths 

lub słowniki

>>> format_dictionary = {'births': 'five', 'deaths': 'three'} 
>>> print "there are {births} births, and {deaths} deaths".format(**format_dictionary) #yup, unpack the dictionary 
there are five births, and three deaths 
1

Skopiowałem i wkleiłem twój skrypt do pliku .py. Uruchomiłem go tak jak jest w Pythonie 2.7.10 i otrzymałem ten sam błąd składni. Próbowałem również skrypt w Pythonie 3.5 i otrzymała następujące wyniki:

File "print_strings_on_same_line.py", line 16 
print fiveYears 
      ^
SyntaxError: Missing parentheses in call to 'print' 

Potem zmodyfikowane ostatnią linię w którym drukuje się numer urodzeń w następujący sposób:

currentPop = 312032486 
oneYear = 365 
hours = 24 
minutes = 60 
seconds = 60 

# seconds in a single day 
secondsInDay = hours * minutes * seconds 

# seconds in a year 
secondsInYear = secondsInDay * oneYear 

fiveYears = secondsInYear * 5 

#Seconds in 5 years 
print fiveYears 

# fiveYears in seconds, divided by 7 seconds 
births = fiveYears // 7 

print "If there was a birth every 7 seconds, there would be: " + str(births) + " births" 

Wydajność była (Python 2.7.10)

157680000 
If there was a birth every 7 seconds, there would be: 22525714 births 

Mam nadzieję, że to pomoże.

3

Na aktualnej wersji Pythona trzeba używać nawiasów, tak:

print („Jeśli nie było urodzenie co 7 sekund”, x)

10

Jeśli chcesz pracować z Pythona 3, to bardzo proste:

print("If there was a birth every 7 second, there would be %d births." % (births)) 
2

byłoby najpierw zrobić zmienną, na przykład: D = 1. Wtedy to zrobić, ale zastąpi ciąg z cokolwiek chcesz:

D = 1 
print("Here is a number!:",D) 
Powiązane problemy