Im trying to get this program running but i cant seem to get it working the way i want it to, any help ?
Currently having a VauleIndx error for- ValueError: invalid literal for int() with base 10: 'Whats your sentence ?'
Im not to sure how to actually fix this, i thought at first to change the ask_user but im not sure if i should add something there or not.
Also im trying to answer this question/code
This program will require you to take a string input from a user and rearrange the string in the reverse order, displaying the output to the user. The obvious first approach will be to simply reverse all the string characters, but you can further enhance this project by changing various program attributes: o Take a sentence as the input and reverse each word in the sentence, keeping the word positions the same o Take a sentence as the input and reverse the order in which the words appear without changing the content of the words themselves. Similarly, a lot of other variations of string manipulation can be implemented in this assignment. At the end of it, you can even create a menu-based program where the users can select what sort of string manipulation they want to perform on their given inputs.
#set define items
choice = None
import sys
#Set generic question and response
def ask_user (question) :
response = int(input(question))
return response
#set generic ask for a number
def ask_number (question, low, high) :
#Ask for a number in a range
response = None
while response not in range(low, high) :
response = int(input(question))
return response
#define, reverse each word in the sentence, keeping the word positions the same
def reverse_word(Sentence):
words = Sentence.split(" ")
newWords = [word[::-1] for word in words]
newSen = " ".join(newWords)
return newSen
#Print the name and sentence for the radomizer
def display_instruct():
print (
"""
Welcome to the sentence radomizer
"""
)
print (display_instruct())
sentence = ask_user("Whats your sentence ?")
#Print the main menu for the person to see and choice from which randomizer they want
def display_instruct2():
print (
"""
Pick the randomizer corresponding to the you want,
1 - Just reverse the words, keep postion same
2 -
3 -
4 -
5 - Exit
"""
)
print (display_instruct2())
while choice not in range (1, 6):
choice = ask_number("What number do you choose from the list?", )
if choice == 1:
print(reverse_word(Sentence))
#elif choice == 2:
#
#elif choice == 3:
#
#elif choice == 4:
#
elif choice == 5:
print ("Cya nerd")
sys.exit() ```
CodePudding user response:
On Line 7 you have wrapped your input statement with int(..). As the user input is a sentence, the program throws a ValueError as it cannot convert a string to an int. To resolve the error, replace int(input(question)) with input(question) as below;
def ask_user(question):
response = input(question)
return response
CodePudding user response:
#set define items
choice = None
import sys
#Set generic question and response
def ask_user (question) :
response = input(question)
return response
#set generic ask for a number
def ask_number (question, low, high) :
#Ask for a number in a range
response = None
while response not in range(low, high) :
response = int(input(question))
return response
#Print the name and sentence for the radomizer
def display_instruct():
print(
"""
Welcome to the sentence radomizer
"""
)
display_instruct()
Sentence = ask_user("Whats your sentence ?")
#define, reverse each word in the sentence, keeping the word positions the same
def reverse_word(Sentence):
words = Sentence.split(" ")
newWords = [word[::-1] for word in words]
newSen = " ".join(newWords)
return newSen
#Print the main menu for the person to see and choice from which randomizer they want
def display_instruct2():
print(
"""
Pick the randomizer corresponding to the you want,
1 - Just reverse the words, keep postion same
2 -
3 -
4 -
5 - Exit
"""
)
display_instruct2()
while choice not in range(6):
choice = ask_number("What number do you choose from the list?", low=0, high=6)
if choice == 1:
print(reverse_word(Sentence))
#elif choice == 2:
#
#elif choice == 3:
#
#elif choice == 4:
#
elif choice == 5:
print("Cya nerd")
sys.exit()
CodePudding user response:
For your error,
ValueError: invalid literal for int() with base 10: 'Whats your sentence > ?'
Inside ask_user(question)
, you wrap an input
statement inside int()
. Since your input (Whats your sentence ?
) is definitely not convertible to int
, it causes the error above. I'd suggest you remove the int
.
def ask_user(question):
response = input(question)
return response
In addition to that, your functions display_instruct1()
and display_instruct2()
don't return any value (return None
), your statements print(display_instruct1())
and print(display_instruct2())
won't display any useful information. You can either change the print
inside each function to return
instead, or you can forego the two outer print
statements. I'd suggest you do the latter, because your functions are named display
, you shouldn't be expected to display anything by yourself.