2017-03-03 3 views
0

Ich habe versucht, diesen Code für immer zu beheben, aber ich bin nicht erfolgreich, ich versuche, es zu machen, sobald Sie geantwortet haben. Das Programm wird automatisch neu gestartet und Sie können es erneut spielen. Doch nach mehreren Versuchen habe ich nicht in der Lage gewesen, diese10 Schleifenanweisungen mit Eingabe

question = input("Press 'r' for rock, 'p' for paper or 's' for scissors") 
if question is 'r': 
    print('I choose paper, I win!') 
elif question is 's': 
    print('I choose rock, I win!') 
elif question is 'p': 
    print('I choose scissors, I win!') 

for restart in question: 
    print(restart) 
+0

Bitte den tatsächlichen Code anstelle von Screenshots einfügen. Und bekommen Sie irgendwelche Fehler? – AKS

+2

Was ist "Neustart" in Ihrem Programm? –

+0

@AKS Da ich meinen Code nicht ohne Fehler einfügen konnte, musste ich auf einen Screenshot zurückgreifen. Der Code funktioniert einwandfrei, aber wenn ich es versuche und neu starte. Es zeigt die Eingabe an, die der Benutzer zuvor gemacht hat, anstatt das Programm tatsächlich neu zu starten. – Sid

Antwort

1

Versuchen Sie folgendes:

while (True): 
    question = input("Press 'r' for rock, 'p' for paper or 's' for scissors or press 'e' to exit:") 
    if question is 'r': 
     print('I choose paper, I win!') 
    elif question is 's': 
     print('I choose rock, I win!') 
    elif question is 'p': 
     print('I choose scissors, I win!') 
    elif question is 'e': 
     print('restart') 
     break 
-1

Sie sollten zu tun, legen Sie Ihre input-Anweisung in einer Schleife

question = "" 
while(question != 'e'): 
    question = input("Press e to exit") 
    if question is 'r': 
     print("....r") 
    elif question is 's': 
     print(".....s")` 
+1

wer kann mir erklären, warum jemand diese Antwort downwog? – latsha

0

Sie können eine Codestruktur haben wie folgt.

while True: 
    question = input("Press 'r' for rock, 'p' for paper or 's' for scissors") 
    if question is 'r': 
     print('I choose paper, I win!') 
    elif question is 's': 
     print('I choose rock, I win!') 
    elif question is 'p': 
     print('I choose scissors, I win!') 

    if condition: # write your condition here to exit from loop 
     break 
0

diesen Code versuchen:

def restart(): 
    question = input("Press 'r' for rock, 'p' for paper or 's' for scissors") 
    if question is 'r': 
     print('I choose rock, I win!') 
    elif question is 's': 
     print('I choose scissors, I win!') 
    elif question is 'p': 
     print('I choose papers, I win!') 
    else: 
     print('wrong choice') 


continuePlay = 'y' 
while continuePlay is 'y': 
    restart()  
    continuePlay = input("Press 'y' for Yes, 'n' for No") 
0

Sie Funktion nutzen zu können.

def my_game(): 
    question = input("Press 'r' for rock, 'p' for paper or 's' for scissors") 
    if question is 'r': 
     print('I choose paper, I win!') 
    elif question is 's': 
     print('I choose rock, I win!') 
    elif question is 'p': 
     print('I choose scissors, I win!') 

    for restart in question: 
     print(restart) # this command will print the string entered by the user, one character in each line 

while True: 
    my_game()