मैं कोडिंग के लिए नया हूँ। यह तब तक ठीक काम करता है जब तक आप किसी संख्या का अनुमान नहीं लगाते। तब यह या तो उच्च या निम्न सदा के लिए कहता है। कृपया मुझे यह समझने में मदद करें कि मैंने क्या गलत किया है। मैंने इंटरनेट पर खोज करने और कुछ कोड को फिर से टाइप करने का प्रयास किया है लेकिन यह काम नहीं करेगा। मैं एक बातचीत के साथ-साथ एक मिनी अनुमान संख्या गेम बनाने की कोशिश कर रहा हूं।
यह रहा मेरा कोड
# Conversation A
import random
print("Answer all questions with 'yes' or 'no' and press ENTER")
print('Hello I am Jim the computer')
z = input('How are you? ')
if z == 'Good' or z == 'Great' or z == 'good' or z == 'great':
print("That's great!")
else:
print("Oh. That's sad. I hope you feel better.")
a = input("what's your name? ")
print(a + "? Thats a nice name.")
b = input('Do you own any pets? ')
if b == 'yes' or b == 'Yes':
print("That's cool. I love pets!")
else:
print("That's a shame, you should get a pet. ")
c = input("Do you like animals? ")
if c == 'yes' or c == 'Yes':
print("Great! Me too!")
else:
print("Oh that's sad. They're really cute. ")
d = input("Do you want to see a magic trick? ")
if d == "yes" or d == "Yes":
input("Ok! Think of a number between 1-30. Do you have it? ")
print("Double that number.")
print("Add ten.")
print("Now divide by 2...")
print("And subtract your original number!")
y = input("Was your answer 5? ")
if y == 'yes' or y == 'Yes':
print("Yay i got it right! I hope you like my magic trick.")
else:
print("Oh that's sad. I'll work on it. I really like magic tricks and games.")
else:
print("Ok. Maybe next time. I love magic tricks and games!")
e = input("Do you want to play a game with me? ")
if e == "yes" or e == "Yes":
randnum = random.randint(1,100)
print("I am thinking of a number between 1 and 100...")
if e == 'no' or e == 'No':
print("oh well see you next time" + a + '.')
guess = int(input())
while guess is not randnum:
if guess == randnum:
print("Nice guess " + a + "! Bye, have a nice day!")
if guess < randnum:
print("Higher.")
if guess > randnum:
print("Lower.")
1 उत्तर
जब आप लूपिंग बंद करना चाहते हैं तो आपको एक break
स्टेटमेंट जोड़ना होगा और लूप के अंदर input
के लिए guess
को स्थानांतरित करना होगा ताकि आप स्टेटमेंट को प्रिंट करने से पहले बाहर न निकलें:
while True:
guess = int(input())
if guess == randnum:
print("Nice guess " + a + "! Bye, have a nice day!")
break
संपादित करें: साथ ही, आप हर बार is
का उपयोग नहीं करना चाहते: है "==" और "is" में क्या अंतर है?
संबंधित सवाल
जुड़े हुए प्रश्न
नए सवाल
python
पायथन एक बहु-प्रतिमान है, गतिशील रूप से टाइप किया हुआ, बहुउद्देशीय प्रोग्रामिंग भाषा है। यह एक साफ और एक समान वाक्यविन्यास सीखने, समझने और उपयोग करने के लिए त्वरित होने के लिए डिज़ाइन किया गया है। कृपया ध्यान दें कि अजगर 2 आधिकारिक तौर पर 01-01-2020 के समर्थन से बाहर है। फिर भी, संस्करण-विशिष्ट पायथन सवालों के लिए, [अजगर -२.०] या [अजगर -३.x] टैग जोड़ें। पायथन वेरिएंट (जैसे, ज्योथन, PyPy) या लाइब्रेरी (उदा।, पांडस और न्यूमपी) का उपयोग करते समय, कृपया इसे टैग में शामिल करें।
in
का उपयोग यह जांचने के लिए कर सकते हैं कि क्या कोई स्ट्रिंग कई अलग-अलग स्ट्रिंग्स की कोई है, इसलिएif z in ("Good", "Great", "good", "great")
उन सभी को लिखे बिना उसी तरह व्यवहार करेगाor
और==
स्टेटमेंट। इससे भी बेहतर, आपz.lower() in ("good", "great")
का उपयोग कर सकते हैं।.lower()
विधि स्ट्रिंग का लोअरकेस संस्करण लौटाती है, ताकि आप केस-असंवेदनशील जांच कर सकें।