2012-10-04 19 views
15

मैं अजगर के लिए नया हूं, और अपनी पहली परियोजनाओं में से एक के रूप में एक प्रकार का खेल बना रहा हूं जो 1 और 10 के बीच की संख्या का अनुमान लगाता है, तो उपयोगकर्ता इसे अनुमान लगाता है। उनके पास तीन अनुमान हैं, और कार्यक्रम उपयोगकर्ता को बताता है कि उन्हें अपने अगले अनुमान पर उच्च या निम्न जाने की आवश्यकता है। त्रुटि के साथ कोड का हिस्सा महत्वपूर्ण नहीं है, क्योंकि यह केवल अनुमान लगाता है कि उपयोगकर्ता दो बार एक ही जवाब में रखता है, जिससे उन्हें पहली बार अपना अनुमान दोबारा शुरू करने की इजाजत मिलती है लेकिन फिर से लेने की इजाजत नहीं होती है दूसरा। कोड पर, मैंने चिह्नित किया है कि समस्या कहां है। जैसे मैंने कहा, मैं वास्तव में पाइथन के लिए नया हूं और शायद यह कुछ शौकिया नोबॉ गलती है। अग्रिम में धन्यवाद।एक कथन में कॉलन पर सिंटेक्स त्रुटि

import time # This imports the time module. 
import random # This imports the random module. 

MyNumber = random.randrange(1,10) # This picks a number for the variable 'MyNumber'. 

# Intro text and instructions. 
print('=====================================') 
print('=Welcome to GuessMyNumber!   =') 
print('=         =') 
print('=I will make a random number between=') 
print('=1 and 10, and you must guess it. If=') 
print('=you are wrong, I will tell you if =') 
print('=you need to go higher or lower. Be =') 
print('=careful, as you only have three =') 
print('=guesses!       =') 
print('=====================================') 
print() 

firstGuess = int(input('Ok then, we shall begin! What is your first guess?')) 
print() 
if firstGuess == (MyNumber): 
print('Well done! You win!') 
time.sleep(3) 
exit() 
if firstGuess < MyNumber: 
print('Go Higher!') 
time.sleep(1) 
if firstGuess > MyNumber: 
print('Go Lower!') 
time.sleep(1) 

print() 
secondGuess = int(input('Better luck this time! What is your second guess?')) 
print() 
if secondGuess == firstGuess: 
print('You tried that one last time! Don\'t worry, I won\'t count that one!') 
bungled = (1) 
secondGuess = int(input('What is your second guess?') 
if secondGuess == firstGuess:#This colon is causing the problem. 
    print('You\'ve already tried that one twice!') 
    bungled = (2) 
if secondGuess == MyNumber: 
print('Well done! You win!') 
time.sleep(3) 
exit() 
if secondGuess < MyNumber: 
print('Go Higher!') 
time.sleep(1) 
if secondGuess > MyNumber: 
print('Go Lower!') 
time.sleep(1) 

print() 
thirdGuess = int(input('This is your final chance! What is your third guess?')) 
print() 
if thirdGuess == MyNumber: 
print('Well done! You win!') 
time.sleep(3) 
exit() 
if thirdGuess < MyNumber: 
MyNumber = str(MyNumber) 
print('Sorry! You lost! The number was '+MyNumber) 
time.sleep(1) 
exit() 
if thirdGuess > MyNumber: 
MyNumber = str(MyNumber) 
print('Sorry! You lost! The number was '+MyNumber) 
time.sleep(1) 
exit() 

उत्तर

18

यह वास्तव में पेट नहीं है। यह पिछली लाइन पर अनजान ब्रैकेट है।

जब आप एक अजीब SyntaxError प्राप्त करते हैं, तो इससे पहले ब्रैकेट संतुलन की जांच करें।

+0

धन्यवाद, जब मैं फिर से एक अजीब त्रुटि प्राप्त करता हूं तो मैं हमेशा ब्रैकेट की जांच करूंगा। – Chimp

2

उपरोक्त पंक्ति में एक कोष्ठक गुम है। बदलें

secondGuess = int(input('What is your second guess?')

को

secondGuess = int(input('What is your second guess?'))

+0

बहुत बहुत धन्यवाद, मुझे यकीन नहीं है कि चेकर्स ने इसे क्यों नहीं उठाया। सबकुछ अब पूरी तरह से काम करता है :) – Chimp

संबंधित मुद्दे