python - Generating random numbers in Python using random.randrange(). After the final number is generated, a "%" is created. How can I fix this?
问题描述
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
This is the output I get when I enter 1, for example:
You selected 1. The following 3-digit lottery number was generated:
657%
How can I get rid of this percentage sign? I am using VSCode to compile. Thanks.
解决方案
% 不是 Python 的输出。这是您的外壳程序告诉您没有尾随换行符(或者,更确切地说,光标停留在非零列偏移处;当该输出未被捕获或重定向时,您的外壳程序无法看到它运行的程序写入的输出,但它可以查询终端的光标位置)。您退出前的最后一次打印不应该有end=''
.
推荐阅读
- javascript - 在 Angular 中从 Firebase 中检索 UID
- android-studio - 如何在不转到 xml 文件的情况下为 android studio 中的按钮提供名称和 ID?
- mysql - 触发以验证房间容量
- r - 如何计算 R 中的 ker(A) / 零空间
- vue.js - Vue Template 中的匿名函数是性能杀手吗?
- hibernate - Cascade ALL 不为儿童组合生成 id
- python - python中缺少值的统计信息
- android - 使用 OkHttp 客户端执行原始 graphQL 查询
- java - Kafka Streams并行处理不起作用
- python - /Service /usr/bin/chromium-browser 处的 WebDriverException 意外退出。状态码是:127