如何使python代码更短、更高效

如何使python代码更短、更高效,python,Python,这是我尝试做一个更高/更低的游戏 import random print("A game of Higher or Lower") number = random.randint(1, 100) choice = int(input("Please pick a number between 1 & 100: ")) if choice < number: print("Higher") elif choice > number: print("Lower")

这是我尝试做一个更高/更低的游戏

import random
print("A game of Higher or Lower")
number = random.randint(1, 100)
choice = int(input("Please pick a number between 1 & 100: "))
if choice < number:
    print("Higher")
elif choice > number:
    print("Lower")
else:
    print("Well done!")
while choice != number:
    choice = int(input("Pick again: "))
    if choice < number:
        print("Higher")
    elif choice > number:
        print("Lower")
    else:
        print("Well done!")
随机导入
打印(“高或低的游戏”)
number=random.randint(1100)
choice=int(输入(“请选择一个介于1和100之间的数字:”)
如果选择<编号:
打印(“更高”)
elif选项>编号:
打印(“下”)
其他:
打印(“做得好!”)
而选择!=编号:
choice=int(输入(“再次拾取:”)
如果选择<编号:
打印(“更高”)
elif选项>编号:
打印(“下”)
其他:
打印(“做得好!”)
我是python新手,我只是想知道有没有办法缩短代码以提高效率?不要认为需要两个“if/elif/else”语句,但看不到合并它们的方法。抱歉,如果这是个愚蠢的问题

更新代码:

import random
print("A game of Higher or Lower")
number = random.randint(1, 100)
choice = ""
while choice != number:
    choice = int(input("Please pick a number: "))
    if choice < number:
        print("Higher")
    elif choice > number:
        print("Lower")
    else:
        print("Well done!")
随机导入
打印(“高或低的游戏”)
number=random.randint(1100)
choice=“”
而选择!=编号:
choice=int(输入(“请选择一个数字:”)
如果选择<编号:
打印(“更高”)
elif选项>编号:
打印(“下”)
其他:
打印(“做得好!”)

您可以这样做:

import random
print("A game of Higher or Lower")
number = random.randint(1, 100)
while True:
    try:
        choice = int(input("Please pick a number between 1 & 100: "))
    except ValueError:
        continue
    if choice < number:
        print("Higher")
    elif choice > number:
        print("Lower")
    else:
        print("Well done!")
        break
import random
print("A game of Higher or Lower")

number = random.randint(1, 100)
choice = int(input("Please pick a number between 1 & 100: "))

while choice != number:
    if choice < number:
        print("Higher")
    elif choice > number:
        print("Lower")

    choice = int(input("Pick again: "))

print("Well done")
随机导入
打印(“高或低的游戏”)
number=random.randint(1100)
尽管如此:
尝试:
choice=int(输入(“请选择一个介于1和100之间的数字:”)
除值错误外:
持续
如果选择<编号:
打印(“更高”)
elif选项>编号:
打印(“下”)
其他:
打印(“做得好!”)
打破

这里有一个建议:用保证不相等的
数字初始化
选项
(例如负数,一个“哨兵”)。然后您可以立即开始while循环,因为第一次该条件始终为true

然后可以删除while循环外部的第一个if/Then/else块和对
input()
的第一个调用


顺便说一句,“较短的代码”并不总是“有效的代码”:)

您可以尝试以下方法:

import random
print("A game of Higher or Lower")
number = random.randint(1, 100)
while True:
    try:
        choice = int(input("Please pick a number between 1 & 100: "))
    except ValueError:
        continue
    if choice < number:
        print("Higher")
    elif choice > number:
        print("Lower")
    else:
        print("Well done!")
        break
import random
print("A game of Higher or Lower")

number = random.randint(1, 100)
choice = int(input("Please pick a number between 1 & 100: "))

while choice != number:
    if choice < number:
        print("Higher")
    elif choice > number:
        print("Lower")

    choice = int(input("Pick again: "))

print("Well done")
随机导入
打印(“高或低的游戏”)
number=random.randint(1100)
choice=int(输入(“请选择一个介于1和100之间的数字:”)
而选择!=编号:
如果选择<编号:
打印(“更高”)
elif选项>编号:
打印(“下”)
choice=int(输入(“再次拾取:”)
打印(“做得好”)

封装检查用户输入的代码也可能是一个好主意。

if/else语句可以放在一行:

import random
print("A game of Higher or Lower")
number = random.randint(1, 100)
choice = ""
while choice != number:
    choice = int(input("Please pick a number: "))
    s = 'Higher' if choice < number else ('Lower' if choice > number else 'Well done!')
    print(s)
随机导入
打印(“高或低的游戏”)
number=random.randint(1100)
choice=“”
而选择!=编号:
choice=int(输入(“请选择一个数字:”)
s='Higher'if choicenumber else'做得好!')
印刷品
如果要最小化字符数,请将变量名称更改为“缩写”,如:

随机导入
打印(“高或低的游戏”)
n=random.randint(1100)
c=“”
而c!=n:
c=int(输入(“请选择一个数字:”)
如果cn else,则s=‘较低’‘做得好’)
印刷品

每当你发现自己在复制代码时,问问自己“我能把它放在函数中吗?”找到它。我让“choice”为空字符串,因此当涉及while循环时,它会被定义。我投票将此问题作为主题外的问题结束,因为有关改进工作代码的问题在堆栈溢出上是主题外的。考虑一下在CoDeEvIEW.StAcExchange上的旅行,看看你的问题是否适合,也欢迎到堆栈溢出。你的问题不是一个愚蠢的问题,但一定要查看我们的网站,以便对网站的结构有一个好的了解!如果用户没有输入任何内容,这将引发
ValueError
。但无论如何,它回答了这个问题。@Manjunath我添加了一个try/except来修复这个场景