2016-10-06 2 views
0

私はプログラミングの世界では非常に新しく、いくつかの本当に基本的なプログラムを書いています。だから私が与えられた問題は、先生が学生に仕事を任せて助けを必要とすることです。彼女は無作為に生徒に数字を与えます。生徒はプログラムに自分の番号を入力し、短いストローを作ったかどうかをプログラムに知らせます。プログラムは、どのくらい多くの学生がクラスに入っているかに応じて実行することができなければなりません。これは私が立ち往生している場所であり、クラス内の学生数に応じてプログラムを実行する方法を見つけることができません。ここに私のコードは1で、あなたのコードの残りの部分をラップし、簡単に言えばPythonどのようにコードの特定の部分を再度実行するのですか

import random 

print("Welcome to the Short Straw Game") 
print("This first part is for teachers only") 
print("") 

numstudents = int(input("How many students are in your class: ")) 
task = input("What is the task: ") 
num1 = random.randint(0, numstudents) 
count = numstudents 

print("The part is for students") #Trying to get this part to run again depending on number of students in the class 
studentname = input("What is your name:") 
studentnumber = int(input("What is the number your teacher gave to you: ")) 

if studentnumber == num1: 
    print("Sorry", studentname, "but you drew the short straw and you have to", task,) 
else: 
print("Congratulations", studentname, "You didn't draw the short straw") 
count -= 1 
print("There is {0} starws left to go, good luck next student".format(count)) 
+0

forループの中で 'range()'を調べてください。 –

答えて

1

Read up on for loops.

です:

また
# ... 
print("The part is for students") 

for i in range(numstudents): 
    studentname = input("What is your name:") 
    #... 

、プログラミングの世界へようこそ! :) 頑張って楽しんでね!

+0

ありがとうございましたが、プログラム全体をもう一度実行できる方法があります。教師が完了したい別のタスクを持っていれば、プログラムをもう一度実行できますか? – arnoldmtm

+0

まさにそうです。 :)(あるいは、プログラム全体を 'while'ループで囲むことができるので、効果的に永久に実行されますが、プログラムを終了して再起動する方が意味があります)。 – AKX

関連する問題