Python Forum
How to convert boolean "True" to custom string?
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to convert boolean "True" to custom string?
#1
I am trying to write a rock, paper, scissors function without using if statements, loops or lists
The function prompts the user for choice of player 1 and then choice of player 2, and then it displays;

1."Player 1 wins. That is True. It is a tie. That is not True." If player 1 wins
2."Player 1 wins. That is False. It is a tie. That is not True" If player 2 wins
3.'Player 1 wins. That is False. It is a tie. That is not False' If it is a draw

I am having trouble figuring out a way such that the function prints only the correct statement depending on the inputs

This is how far I got:
def rps_winner():
    p1= input('What choice did player 1 make ?' +'\n'+'Type one of the following options: rock, paper, scissors: ')
    p2= input ('What choice did player 2 make?'+ '\n'+'Type one of the following options: rock, paper, scissors: ')
    
    p1wins= (p1=='rock' and p2== 'scissors') or (p1=='paper' and p2=='rock')
    print ('Player 1 wins. That is True'+'\n'+'It is a tie. That is not True')
    
    p2wins= (p1=='rock' and p2== 'paper') or (p1=='paper' and p2=='scissors')
    print ('Player 1 wins. That is False'+ '\n'+'It is a tie. That is not True')
    
    tie= (p1=='rock' and p2== 'rock') or (p1=='paper' and p2=='paper')or(p1=='scissors'and p2== 'scissors')
    print ('Player 1 wins. That is False'+'\n'+'It is a tie. That is not False')
Reply
#2
Store the items in a list or dictionary. Use the expressions you've got to generate the appropriate index or key (remember, booleans are a type of integer, True = 1, False = 0).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
I can't use lists/dictionaries, if statements or loops
Reply
#4
You seem to be on right track. Now you can use and,or operator to convert boolean to string.
eg-

>>> condition = True
>>> condition2 = False
>>> condition and "This is condtion"
'This is condtion'
>>> condition2 and "This is condtion2"
False
>>> "This wont work" and condition
True
The and operator will return the last true evaluated expression, string in this case.
And the or operator will return the first true evaluated expression.

You can apply this to help you with your case.
First, assign the string to be printed to variables

case_p1wins = 'Player 1 wins. That is True'+'\n'+'It is a tie. That is not True'
Similarly, assign case_p2wins, case_tie.

now do logical and of strings with the conditions. (make sure to keep strings at end and condition at beginning).

''' these will either evaluate to the string or False '''
statement_p1 = p1wins and case_p1wins 
statement_p2 = p2wins and case_p2wins
statement_tie = tie and case_tie
Since the conditions were mutually exclusive you can do logical or to find the correct statement

correct_statement = statement_p1 or statement_p2 or statement_tie
print(correct_statement)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert multiple decimal numbers in string to floats Katy8 6 3,464 Aug-16-2020, 06:06 PM
Last Post: Katy8
  Error could not convert string to float: deadendstreet 4 5,323 Oct-02-2019, 05:49 PM
Last Post: ichabod801
  how to convert list into string Shevach 3 2,587 May-14-2019, 09:51 AM
Last Post: perfringo
  Convert string to a specific number of bytes artblinked 1 2,399 Mar-28-2019, 08:43 PM
Last Post: Larz60+
  ValueError: could not convert string to float: 'Pencil' Balakay97 3 5,853 Mar-08-2018, 07:30 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020