首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用random.randrange()在Python中生成随机数。在生成最终数字后,将创建一个"%“。我该如何解决这个问题呢?

使用random.randrange()在Python中生成随机数。在生成最终数字后,将创建一个"%“。我该如何解决这个问题呢?
EN

Stack Overflow用户
提问于 2020-05-29 09:53:17
回答 1查看 220关注 0票数 0
代码语言:javascript
复制
import random

# Main menu.
print('**** Welcome to the Pick-3, Pick-4 lottery number generator! ****\n\
    \nSelect from the following menu:\n\n1. Generate 3-Digit Lottery number\
    \n2. Generate 4-Digit Lottery number\n3. Exit the Application\n')

# Holds the user's selection in memory.
userInput = int(input())

# If-else statements performs proper operation for the user.
if userInput == 1:
    print('\nYou selected 1. The following 3-digit lottery number was generated:\
        \n\n')
    for i in range(3):
        print(random.randrange(1, 10), end = '')

if userInput == 2:
    print('\nYou selected 2. The following 4-digit lottery number was generated:\
        \n\n')
    for i in range(4):
        print(random.randrange(1, 10), end = '')

if userInput == 3:
    print('\nYou selected 3.\n\nThanks for trying the Lottery Application.\
        \n\n*********************************************************')
    SystemExit

这是我输入1时得到的输出,例如:

您选择了% 1。已生成以下3位数字的彩票号码:

657%

我怎样才能去掉这个百分号?我正在使用VSCode进行编译。谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-29 10:04:52

%不是从Python输出的。这是您的shell告诉您没有尾随换行符(或者,更确切地说,是光标停留在非零列偏移量上;当没有捕获或重定向输出时,您的shell无法看到它运行的程序写入了什么输出,但它可以查询终端以获取光标位置)。退出前的最后一张照片不应该有end=''

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62077416

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档