2013-06-18 2 views
1

그래서 곱셈 게임을 만들려고했습니다. 그래서 그것은 다소 효과가 있습니다. 그러나 옳은 답을 넣었을 때, 틀린 대답을 넣었을 때뿐만 아니라 두 번 잘못되었습니다.파이썬 코드에 어떤 문제가 있습니까? (곱셈 프로그램)

import sys #imports sys 
random1=['1','2','3','4','5','6','7','8','9','10','11','12'] #makes a list with numbers 1-12 
random2=['1','2','3','4','5','6','7','8','9','10','11','12'] #makes another list with numbers 1-12 

from random import choice #gets a random number 
while True: #makes the following code run in a loop 
    print "To exit this game type 'exit'" #prints stuff 
    theyputinstuffhere = raw_input("What is " + choice(random2) + " times " + choice(random1) + "? ") #asks the user things like 1*5 or some other random stuff. 


    if theyputinstuffhere == "exit": #if the user typed in 'exit' then exit the game 
     print "Now exiting game!" #prints stuff 
     sys.exit() #exits 

    elif theyputinstuffhere == int(choice(random2))*int(choice(random1)): #if what i typed is right, print stuff (I think i messed up here!) 
     print "Correct!" #print stuff 
    else: 
     print "Wrong!" #otherwise print stuff 

난 내가 잘못 HELP 무엇을했는지 모르는 !!!!!!!!!!!!! 빨리!!!!!!!!!!!!!!!!!

+1

당신이'choice (random2)'와'choice (random1)'을 다시 부를 때 – stackErr

+0

와우 .. .. – karthikr

답변

1

이것은 당신이 생각 가까웠다 목록이 필요

import sys 
import random 

while True: 
    num1 = random.randint(0,12) 
    num2 = random.randint(0,12) 
    print "To exit this game type 'exit'" 
    theyputinstuffhere = raw_input("What is " + str(num2) + " times " + str(num1) + "? ") 


    if theyputinstuffhere == "exit": #if the user typed in 'exit' then exit the game 
     print "Now exiting game!" #prints stuff 
     sys.exit() #exits 

    elif int(theyputinstuffhere) == num1*num2: 
     print "Correct!" #print stuff 
    else: 
     print "Wrong!" 

완벽하게 다음 해달라고 사용 random.randint 대신 선택의 원인을 작동합니다!

와 문 지금은 모든 루프

+0

어때 출구 어때? 그것은 입력과 함께 작동하지 않습니다 ... – user2468523

+0

그는'input' 대신'raw_input'을 의미하지만, 그렇지 않은 경우 올바른 방법입니다. –

+0

퇴장은 어떨까요? 그것은 입력과 함께 작동하지 않습니다 ... – user2468523

0

대신

theyputinstuffhere = raw_input("What is " + choice(random2) + " times " + choice(random1) + "? ") #asks the user things like 1*5 or some other random stuff. 


if theyputinstuffhere == "exit": #if the user typed in 'exit' then exit the game 
    print "Now exiting game!" #prints stuff 
    sys.exit() #exits 

elif int(theyputinstuffhere) == int(choice(random2))*int(choice(random1)): #if what i typed is right, print stuff (I think i messed up here!) 
    print "Correct!" #print stuff 
else: 
    print "Wrong!" #otherwise print stuff 

은 이렇게 : 당신은 두 번 선택을 사용하는

num1 = choice(random1) 
num2 = choice(random2) 
theyputinstuffhere = raw_input("What is " + num1 + " times " + num2 + "? ") #asks the user things like 1*5 or some other random stuff. 


if theyputinstuffhere == "exit": #if the user typed in 'exit' then exit the game 
    print "Now exiting game!" #prints stuff 
    sys.exit() #exits 

elif theyputinstuffhere == int(num1)*int(num2): #if what i typed is right, print stuff (I think i messed up here!) 
    print "Correct!" #print stuff 
else: 
    print "Wrong!" #otherwise print stuff 
+0

Thanks stackErr, 하지만이 작동하지 않습니다 ... 그것은 나를 준다 :이 게임 유형을 종료하려면 '출구' 5 번 7 무엇입니까? 35 잘못되었습니다! 이 게임 유형을 종료하려면 'exit' 5 번 7은 무엇입니까? 2 잘못되었습니다! 이 게임 유형을 종료하려면 'exit' 5 번 7은 무엇입니까? 35 잘못되었습니다! 이 게임 유형을 종료하려면 'exit' 5 번 7은 무엇입니까? – user2468523

+0

나는 아직도 도움이 필요합니다! – user2468523

+0

사실, 내가 잘못한 곳에서 물건을 가지고, 지금은 말 : – user2468523

1

,이 같은 수 무엇을 요구하면서 : 그럼 5 * 6
귀하의 경우 문을 수 같은 것을 확인하십시오 : 11 * 4
또한 int를 문자열과 비교하므로 theyputinstuffhere를 조건부 int로 변환하십시오.

또한 선택 사항()은 randrange()를 사용한 경우 목록을 전혀 만들지 않아도 좋지 않은 함수입니다. 초기 선택() 반환 값을 모두 변수에 할당하고 나중에 프롬프트와 조건부 모두에서 값을 사용하십시오.

+0

또한'raw_input'은 문자열을 돌려 줄 것이므로 대신 int를 얻기 위해'input'을 사용하거나 대답 체크를 위해 int (theyputinstuffhere)를 사용하고 단지 – Serial

+0

예, 편집을 위해서만'theyputinstuffhere'를 사용하십시오. :) –

+0

제발, 이해가 안 돼요 ... 너도 알다시피 ... 나에게 전체 코드를 입력하면 될까? – user2468523

0

이 질문에 대한 답이 >>>>>> 기독교 CAREAGA 응답 IS를 재설정하는 경우 개봉을 사용했을 때 당신은 num1num2을 다시 설정하라는되었다 !!!!!

import sys 
import random 

while True: 
    num1 = random.randint(0,12) 
    num2 = random.randint(0,12) 
    print "To exit this game type 'exit'" 
    theyputinstuffhere = raw_input("What is " + str(num2) + " times " + str(num1) + "? ") 


if theyputinstuffhere == "exit": #if the user typed in 'exit' then exit the game 
    print "Now exiting game!" #prints stuff 
    sys.exit() #exits 

elif int(theyputinstuffhere) == num1*num2: 
    print "Correct!" #print stuff 
else: 
    print "Wrong!" 

감사합니다 !!!! 너 모두 도와 줬어 !!! (죄송합니다 Christian, 나는 방금 잘못을 ...)

관련 문제