2016-09-30 7 views
0

Es ist eine Frage für meine Aufgabe im Intro Programmierung, und ich verstehe nicht ganz, wie dies ohne den Einsatz von Ifs zu tun, weil unser Prof nur Grundmodul und Division will. Ich versuche, 3 Ausgänge zu bekommen. Ballons größer als Kinder (was funktioniert), Ballons gleich Kinder, die nur 0 und 0 ausgibt und Ballons weniger als Kinder, was nicht funktioniert.Python 2.7 Berechnung Rest mit Division und zwei Eingaben

# number of balloons 
children = int(input("Enter number of balloons: ")) 

# number of children coming to the party 
balloons = int(input("Enter the number of children coming to the party: ")) 

# number of balloons each child will receive 
receive_balloons = int(balloons % children) 

# number of balloons leftover for decorations 
remaining = children % balloons 

print("{:s}""{:d}""{:s}""{:d}".format("Number of balloons for each child is ", receive_balloons, " and the amount leftover is ", remaining)) 

print(balloons, "", (remaining)) 
+0

Was verwenden denken Sie, '%' tut? Keine Ihrer Anwendungen davon ist richtig. – user2357112

+0

Teilt es es nicht und berechnet den Rest? – roysizzle

+0

Es tut, also ist Ihr Problem wahrscheinlich mit der Mathematik. Warum denkst du, dass der * Rest * beim Teilen der Anzahl der Ballons durch die Anzahl der Kinder die Anzahl der Ballons ist, die jedes Kind erhält? Warum teilst du die Anzahl der * Kinder * durch die Anzahl der * Ballons * auf und nimmst den Rest, um die Anzahl der übriggebliebenen Ballons zu finden? – user2357112

Antwort

1

Sie benötigen eine variable Zuordnung zu beheben, zuweisen Sie auf die falschen Variablen und tatsächlich die Zahlen teilen receive_balloons richtig zu erhalten:

balloons = int(input("Enter number of balloons: ")) 
children = int(input("Enter the number of children coming to the party: ")) 

receive_balloons = balloons // children 
remaining = balloons % children 

# Alternatively 
receive_balloons, remaining = divmod(balloons, children) 

print("Number of balloons for each child is {} and the amount leftover is {}".format(receive_balloons, remaining)) 

Ausgang (10/5):

Enter number of balloons: 10 
Enter the number of children coming to the party: 5 
Number of balloons for each child is 2 and the amount leftover is 0 

Ausgang (10/8):

Enter number of balloons: 10 
Enter the number of children coming to the party: 8 
Number of balloons for each child is 1 and the amount leftover is 2 

Hinweis: In Python2.7 sollten Sie raw_input verwenden.

+0

Also habe ich die Variablen in der richtigen Reihenfolge geschaltet und jetzt die Ballons erhalten nur 0, was ist, was ich zuvor hatte Anzahl der Kinder eingeben: 10 Geben Sie die Anzahl der Ballons: 5 Anzahl der Ballons für jedes Kind ist 0 und die Menge übrig ist 5 – roysizzle

+0

Sorry nicht in der Lage, Ihr Problem zu replizieren ... siehe Ausgabe oben. – AChampion

+0

Wenn ich raw_input verwende bekomme ich eine undefinierte Variable – roysizzle

1

Sie müssen den // Operator für die Anzahl der Ballons pro Kind und% Rest Ballons

# number of balloons 
balloons = int(input("Enter number of balloons: ")) 

# number of children coming to the party 
children = int(input("Enter the number of children coming to the party: ")) 

receive_balloons, remaining = (balloons // children, balloons % children) 

print("{:s}""{:d}""{:s}""{:d}".format("Number of balloons for each child is ", receive_balloons, " and the amount leftover is ", remaining)) 

print(balloons, "", (remaining)) 
+0

Dies funktioniert, aber wenn es für mein Wissen möglich ist, erklären Sie die receive_balloons, Rest = (Ballons // Kinder, Ballons% Kinder)? Ich wusste nicht, Sie könnten eine Doppelberechnung tun – roysizzle

+0

Sie können ein 'Tupel' in Zuordnung, z. 'a, b = (1, 2)' ist gleich 'a = 1' und 'b = 2', der obige Code erzeugt ein 'Tupel' der Werte von Division und Modulus und ordnet sie den Variablen zu. Syntethischer Zucker. – AChampion

+0

In dem Beispiel entpackt die Zuweisung auch ein Tupel. Das Tupel ist das Bit in den Klammern. Jedes Element des Tupels wird individuell einer Variablen zugewiesen. –