I'm taking the CS50 course and right now I'm on pset6, python Mario code.
For the rstrip() function, it's supposed to remove the new line at the end of the code. I don't know whats going on and its really bugging me (i think its a syntax error but I'm not sure).
Please if any f you could help that would be great!
Note: I am using "+" symbol to represent " " just for personal understanding.
My code:
def input_number(message):
while True:
try:
user_input = int(input(message))
except ValueError:
continue
else:
return user_input
height = input_number("Height: ")
while height < 1 or height > 8:
height = int(input("Height: "))
i = 0
while i < height:
print("\n")
x = height-1
a = 0
while x>=i:
print("+".rstrip("\n"))
x-=1
while a<=i:
print("#".rstrip("\n"))
a+=1
i+=1
#print("\n")
whats getting printed:
Height: 5
+
+
+
+
+
#
+
+
+
+
#
#
+
+
+
#
#
#
+
+
#
#
#
#
+
#
#
#
#
#
expected output:
#
##
###
####
#####
Thanks!
Use end=""
in print method rather than using rstrip
method.
def input_number(message):
while True:
try:
user_input = int(input(message))
except ValueError:
continue
else:
return user_input
height = input_number("Height: ")
while height < 1 or height > 8:
height = int(input("Height: "))
i = 0
while i < height:
x = height-1
a = 0
while x>=i:
print(" ", end="")
x-=1
while a<=i:
print("#", end="")
a+=1
print("")
i+=1
Output:
Height: 5
#
##
###
####
#####
Explanation:
rstrip
is used to remove white spaces from end of a string. For example, when we want to remove unwanted white spaces from user inputted string, we can use rstrip
to do so. Another use case of rstrip
is to omit white spaces from lines while reading a file.
On the other hand, to format a output string in a desired format we manipulate print
method. By default, print
method outputs the values in a separate line.
For example,
print("some string")
print("another string")
It will show:
some string
another string
According to print method documentation , we can use string in end
argument to overwrite the default end='\n'
.
For example,
print("some string", end="......")
print("another string", end="!!!!!!")
It will show:
some string......another string!!!!!!
Reference:
It appears that you'd like to remove the \n
at the end of the line.
Instead of rstrip use print("whatever you want to output in here like # or +", end = "")
.
The reason why rstrip won't work in this usecase is that it strips the string you passed to it which is "#" or "+". There is no \n
there, that is added as the end character of print.
def input_number(message):
while True:
try:
user_input = int(input(message))
except ValueError:
continue
else:
return user_input
height = input_number("Height: ")
while height < 1 or height > 8:
height = int(input("Height: "))
i = 0
while i < height:
print("")
x = height-1
a = 0
# while x>=i:
# print("+", end = "")
# x-=1
while a<=i:
print("#", end = "")
a+=1
i+=1
Output:
#
##
###
####
#####
# TODO
while True:
n = int(input("Height: "))
if n > 0 and n < 9:
break
for i in range(0, n, 1):
for j in range(0, n, 1):
if (i+j < n-1):
print(" ", end="")
else:
print("#", end="")
print()
here thats my answer to your question.
the Output is the same :
#
##
###
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.