Python代码遵循if语句[关闭](Python code follow the if statements [closed])

编程入门 行业动态 更新时间:2024-10-26 14:39:35
Python代码遵循if语句[关闭](Python code follow the if statements [closed])

我似乎无法在Python 2.7中运行a == x [3]。 我输入了正确的数字并且没有说'恭喜'我告诉它说声明是否正确。 另外,如何重复十次而不必复制和粘贴代码十次。 非常感谢提前!

import time import random x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46,47,48,49,50] print "WELCOME TO THE BEST GUESSING GAME IN THE WORLD! GUESS THE RIGHT NUMBER AND YOU WIN A BRAND-NEW CAR" random.shuffle(x) time.sleep(2) print "YOU GET 10 TRIES" time.sleep(1) print "GOOD LUCK!" time.sleep(2) a = raw_input('Enter number 1-50 ') time.sleep(2) if a == x[3]: print 'Congrats' else: print 'NO' a = raw_input('Enter number 1-50 ') time.sleep(2) if a == x[3]: print 'Congrats' else: print 'NO' a = raw_input('Enter number 1-50 ') time.sleep(2) if a == x[3]: print 'Congrats' else: print 'NO' a = raw_input('Enter number 1-50 ') time.sleep(2) if a == x[3]: print 'Congrats' else: print 'NO' a = raw_input('Enter number 1-50 ') time.sleep(2) if a == x[3]: print 'Congrats' else: print 'NO' a = raw_input('Enter number 1-50 ') time.sleep(2) if a == x[3]: print 'Congrats' else: print 'NO' a = raw_input('Enter number 1-50 ') time.sleep(2) if a == x[3]: print 'Congrats' else: print 'NO' a = raw_input('Enter number 1-50 ') time.sleep(2) if a == x[3]: print 'Congrats' else: print 'NO' a = raw_input('Enter number 1-50 ') time.sleep(2) if a == x[3]: print 'Congrats' else: print 'NO' a = raw_input('Enter number 1-50 ') time.sleep(2) if a == x[3]: print 'Congrats' else: print 'NO' a = raw_input('Enter number 1-50 ') time.sleep(2) if a == x[3]: print 'Congrats' else: print 'NO' time.sleep(1) print 'YOU FAILED!!' print x[3]

I can't seem to get the a == x[3] to run as it should in Python 2.7. I enter in the correct number and it doesn't say the 'Congrats' I told it to say if the statement was correct. Also, how can I make repeat ten times instead of having to copy and paste the code ten times. Thanks so much in advance!

import time import random x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46,47,48,49,50] print "WELCOME TO THE BEST GUESSING GAME IN THE WORLD! GUESS THE RIGHT NUMBER AND YOU WIN A BRAND-NEW CAR" random.shuffle(x) time.sleep(2) print "YOU GET 10 TRIES" time.sleep(1) print "GOOD LUCK!" time.sleep(2) a = raw_input('Enter number 1-50 ') time.sleep(2) if a == x[3]: print 'Congrats' else: print 'NO' a = raw_input('Enter number 1-50 ') time.sleep(2) if a == x[3]: print 'Congrats' else: print 'NO' a = raw_input('Enter number 1-50 ') time.sleep(2) if a == x[3]: print 'Congrats' else: print 'NO' a = raw_input('Enter number 1-50 ') time.sleep(2) if a == x[3]: print 'Congrats' else: print 'NO' a = raw_input('Enter number 1-50 ') time.sleep(2) if a == x[3]: print 'Congrats' else: print 'NO' a = raw_input('Enter number 1-50 ') time.sleep(2) if a == x[3]: print 'Congrats' else: print 'NO' a = raw_input('Enter number 1-50 ') time.sleep(2) if a == x[3]: print 'Congrats' else: print 'NO' a = raw_input('Enter number 1-50 ') time.sleep(2) if a == x[3]: print 'Congrats' else: print 'NO' a = raw_input('Enter number 1-50 ') time.sleep(2) if a == x[3]: print 'Congrats' else: print 'NO' a = raw_input('Enter number 1-50 ') time.sleep(2) if a == x[3]: print 'Congrats' else: print 'NO' a = raw_input('Enter number 1-50 ') time.sleep(2) if a == x[3]: print 'Congrats' else: print 'NO' time.sleep(1) print 'YOU FAILED!!' print x[3]

最满意答案

raw_input将返回一个字符串。 你需要先把它转换为int! 使用int(raw_input('...')) 。

另外, int()在你向它提供无法转换为字符串的东西时会崩溃,所以你可能希望用一个处理不正确输入的try/catch语句来包围它以防止意外崩溃。

你可以使用for循环来避免复制粘贴代码。 如果你是计算机科学的新手并正在学习python,请查看非常受欢迎的Learn Python the Hard Way 。

raw_input is going to return a string. you'll need to convert it to an int first! use int(raw_input('...')).

also, int() crashes when you feed it things that it cannot cast to strings, so you may want to surround it with a try/catch statement that deals with improper inputs to prevent unexpected crashes.

you can use a for loop to avoid copy-pasting code. if you're new to computer science and are learning python, check out the very popular Learn Python the Hard Way.

更多推荐

本文发布于:2023-04-27 23:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329635.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语句   代码   Python   code   closed

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!